package com.tjing.bussiness.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.aliyun.oss.ClientConfiguration;
import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.OSSException;
import com.google.common.collect.Lists;
import com.tjing.bussiness.model.*;
import com.tjing.frame.controller.ToolController;
import com.tjing.frame.model.User;
import com.tjing.frame.services.DbServices;
import com.tjing.frame.util.CodeHelper;
import com.tjing.frame.util.EncryptUtils;
import com.tjing.frame.util.NetworkUtil;
import common.tool;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadBase;
import org.apache.commons.fileupload.ProgressListener;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.lang.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.HashMap;
import java.util.stream.Collectors;

import org.apache.shiro.session.Session;
import org.springframework.web.multipart.MultipartFile;
import util.Constants;
import util.HttpUtils;
import util.PayUtil;
import util.XmlUtil;

@RestController
@RequestMapping("/appapi")
public class AppApiController {

    @Autowired
    private DbServices dbServices;

    Map resultMap = new HashMap();
    HashMap<Integer, Object> intParamMap = new HashMap<Integer, Object>();
    String hql="";
    String returnId="";
    int i = 0;
    Date nowTime=new Date();
    SimpleDateFormat time=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String strnowTime= time.format(nowTime);

    int PageSize = 10;
    int CurrPage=1;
    int RdCount = 0;
    int PageCount = 0;
    int sid = 0;
    int eid = 0;

    private static org.apache.log4j.Logger logger= org.apache.log4j.Logger.getLogger(AppApiController.class);

    @RequestMapping("/appapi")
    public @ResponseBody Map getAppApi(HttpServletRequest request,HttpServletResponse response) throws Exception {
        JSONObject param = CodeHelper.getRequestParams(request);
        String fun=param.getString("fun");

        if(!fun.equals("getQQG")&&!fun.equals("isShowReadDotted")&&!fun.equals("getInProfit")&&!fun.equals("getInAdv")&&!fun.equals("activation")&&!fun.equals("forgetPwd")&&!fun.equals("getCode")&&!fun.equals("login")&&!fun.equals("register")&&!fun.equals("wechatLogin")&&!fun.equals("isRegister")&&!fun.equals("getGuide")&&param.getString("uid")==null){
            resultMap.put("state", "0");
            resultMap.put("msg", "请传uid参数");
            resultMap.put("data", new HashMap());
            return resultMap;
        }
        switch (fun){
            case "login"://登陆
                login(request, response);
                break;
            case "logout"://退出登陆
                logout(request, response);
                break;
            case "register"://注册
                register(request, response);
                break;
            case "getUserInfo"://获取个人信息
                getUserInfo(request, response);
                break;
            case "getInProfit"://首页收益
                getInProfit(request, response);
                break;
            case "getInAdv"://首页广告
                getInAdv(request, response);
                break;
            case "getInviteAdv":
                getInviteAdv(request, response);
                break;
            case "getTaskAdv":
                getTaskAdv(request, response);
                break;
            case "getGuide"://开机引导
                getGuide(request, response);
                break;
            case "getProfitDetail"://收益明细
                getProfitDetail(request, response);
                break;
            case "getProfitList"://收益详情
                getProfitList(request, response);
                break;
            case "getRanking"://获取收入排行
                getRanking(request, response);
                break;
            case "getNoviceTaskList"://新手任务
                getNoviceTaskList(request, response);
                break;
            case "getIndexTaskList"://获取首页任务列表
                getIndexTaskList(request, response);
                break;
            case "getRewardTaskList"://获取奖励任务列表
                getRewardTaskList(request, response);
                break;
            case "getTaskList"://任务列表
                getTaskList(request, response);
                break;
            case "getTaskIngList"://分享中和已结束的任务
                getTaskIngList(request, response);
                break;
            case "getCash"://提现
                getCash(request, response);
                break;
            case "getCashList"://获取提现记录
                getCashList(request, response);
                break;
            case "getCashView"://获取提现详情
                getCashView(request, response);
                break;
            case "shareNoviceTask"://新手任务分享
                shareNoviceTask(request, response);
                break;
            case "share"://分享
                share(request, response);
                break;
            case "read"://阅读
                read(request, response);
                break;
            case "getMentorShip"://获取徒弟关系
                getMentorShip(request, response);
                break;
            case "getMentorShip_banner"://获取直推人数和团队人数
                getMentorShip_banner(request, response);
                break;
            case "getResCond"://获取当前等级的升级剩余条件
                getResCond(request, response);
                break;
            case "getSysParLeaveList"://获取会员等级
                getSysParLeaveList(request, response);
                break;
            case "editAlipay"://绑定支付宝
                editAlipay(request, response);
                break;
            case "modifypwd"://修改密码
                modifypwd(request, response);
                break;
            case "forgetPwd"://忘记密码
                forgetPwd(request, response);
                break;
            case "getQQService"://获取QQ客服
                getQQService(request, response);
                break;
            case "addQhFeedback"://留言反馈
                addQhFeedback(request, response);
                break;
            case "getNoticeList"://获取系统公告
                getNoticeList(request, response);
                break;
            case "getNoticeView"://获取系统公告详情
                getNoticeView(request, response);
                break;
            case "modifyHeadImg"://修改头像
                modifyHeadImg(request);
                break;
            case "modifyUserInfo"://修改用户信息
                modifyUserInfo(request, response);
                break;
            case "realNameCert"://实名认证
                realNameCert(request, response);
                break;
            case "modifyWechat"://修改微信
                modifyWechat(request, response);
                break;
            case "getInviteInfo"://邀请赚钱
                getInviteInfo(request, response);
                break;
            case "setNoticeState"://设置系统公告为已读
                setNoticeState(request, response);
                break;
            case "getCashAccount"://获取提现账户
                getCashAccount(request, response);
                break;
            case "getCode"://获取验证码
                getCode(request, response);
                break;
            case "getNoviceTaskByInviter"://邀请好友
                getNoviceTaskByInviter(request, response);
                break;
            case "getCashRule"://获取提现规则
                getCashRule(request, response);
                break;
            case "getRaiderEntrance"://获取攻略入口
                getRaiderEntrance(request, response);
                break;
            case "getMoneyRaider"://获取赚钱攻略
                getMoneyRaider(request, response);
                break;
            case "getMoneyRule"://获取赚钱规则
                getMoneyRule(request, response);
                break;
            case "getComProblem"://获取常见问题
                getComProblem(request, response);
                break;
            case "getAboutUs"://获取关于钱狐
                getAboutUs(request, response);
                break;
            case "getRegProtocol"://获取注册协议
                getRegProtocol(request, response);
                break;
            case "getAccessToken":
                getAccessToken(request, response);
                break;
            case "updateInviter"://修改推荐人
                updateInviter(request, response);
                break;
            case "getVersion"://获取版本号
                getVersion(request, response);
                break;
            case "getQRcode"://获取二维码
                getQRcode(request, response);
                break;
            case "addAddress"://新增定位
                addAddress(request, response);
                break;
            case "getActivityAdv"://获取活动公告广告
                getActivityAdv(request, response);
                break;
            case "getRecommendInfo":
                getRecommendInfo(request, response);
                break;
            case "wechatLogin"://微信登陆
                wechatLogin(request, response);
                break;
            case "isRegister"://判断是否已注册
                isRegister(request, response);
                break;
            case "getInviteReg"://获取邀请赚钱地址
                getInviteReg(request, response);
                break;
            case "isShare"://判断是否能分享
                isShare(request, response);
                break;
            case "wakeUp"://唤醒
                wakeUp(request, response);
                break;
            case "isShowWakeUp"://是否显示唤醒按钮
                isShowWakeUp(request, response);
                break;
            case "addCommission":
                addCommission();
                break;
            /*case "addCommission1":
                addCommission1();
                break;*/
            case "getDemo":
                getDemo(request, response);
                break;
            case "isShowReadDotted":
                isShowReadDotted(request, response);
                break;
            case "getDemoList"://试玩赚钱列表
                getDemoList(request, response);
                break;
            case "getDemoView"://试玩赚钱详情页
                getDemoView(request, response);
                break;
            case "DemoDownLoad"://下载
                DemoDownLoad(request, response);
                break;
            case "isShowDownLoad"://是否显示下载按钮
                isShowDownLoad(request, response);
                break;
            case "contishare"://继续分享
                contishare(request, response);
                break;
            case "getQhSysPar":
                getQhSysPar(request, response);
                break;
            case "activation"://记录激活
                activation(request, response);
                break;
            case "getQQG":
                getQQG(request, response);
                break;
            case "addInMobile":
                addInMobile(request, response);
                break;
            case "getInviteList":
                getInviteList(request, response);
                break;
            case "isShowActivity":
                isShowActivity(request, response);
                break;
            case "getActivity":
                getActivity(request, response);
                break;
            case "receive"://做完限时活动，领取红包
                receive(request, response);
                break;
            case "createSign":
                createSign(request, response);
                break;
            case "getSign":
                getSign(request, response);
                break;
            case "getSignInfo":
                getSignInfo(request, response);
                break;
            case "isSign"://今天是否签到
                isSign(request, response);
                break;
            case "getTaskCentre":
                getTaskCentre(request, response);
                break;
            case "sendInvite"://发送邀请行为
                sendInvite(request, response);
                break;
            case "addTaskCentre":
                addTaskCentre(request, response);
                break;
            case "getUrl"://获取域名
                getUrl(request, response);
                break;
            default:
                break;
        }
        return  resultMap;
    }

    public void getQhSysPar(HttpServletRequest request,HttpServletResponse response) throws Exception {

        QhSysPar qhSysPar=dbServices.getEntity(QhSysPar.class, 1);
        resultMap.put("state", "1");
        resultMap.put("msg", "");
        resultMap.put("data", qhSysPar);
    }

    //登陆
    public void login(HttpServletRequest request,HttpServletResponse response) throws Exception {
        JSONObject param = CodeHelper.getRequestParams(request);
        String mobile=param.getString("mobile");
        String srcpla=param.getString("srcpla");

        QhSysPar qhSysPar=dbServices.getEntity(QhSysPar.class,1);

        StringBuffer url = request.getRequestURL();
        String tempContextUrl = url.delete(url.length() - request.getRequestURI().length(), url.length()).append(request.getContextPath()).append("/").toString();

        i = 0;
        intParamMap.clear();
        intParamMap.put(i++, mobile);
        intParamMap.put(i++, param.getString("code"));
        hql = " from QhCode where mobile=? and code=?";
        List<QhCode> qhCodes = dbServices.findListByHql(hql, intParamMap);
        if (qhCodes.size() == 0) {
            resultMap.put("state", "0");
            resultMap.put("msg", "验证码不正确");
            resultMap.put("data", new HashMap());
            return;
        }

        hql = "from QhUser where mobile=? ";
        i=0;
        intParamMap.clear();
        intParamMap.put(i++, mobile);

        List<QhUser> result = dbServices.findListByHql(hql,intParamMap);
        if(result.size()>0){
            if(result.get(0).getState()==-1){
                resultMap.put("state", "0");
                resultMap.put("msg", "该用户已暂停使用");
                resultMap.put("data", new HashMap());
            }
            else {
                QhUser qhUser = dbServices.getEntity(QhUser.class, result.get(0).getId());
                JSONObject jsonObj = new JSONObject();

                if(qhUser.getAccessToken()==null||qhUser.getAccessToken().equals("")){
                    qhUser.setAccessToken(ToolController.GetRandLetterString(20));
                    qhUser.setModifyDateTime(new Date());
                    dbServices.update(qhUser);
                }

                jsonObj.put("id",qhUser.getId());
                jsonObj.put("type",qhUser.getType());
                jsonObj.put("uid",qhUser.getUid());
                jsonObj.put("pwd",qhUser.getPwd());
                jsonObj.put("openId",qhUser.getOpenId());
                jsonObj.put("picUrl",qhUser.getPicUrl());
                jsonObj.put("inviter",qhUser.getInviter());
                jsonObj.put("inviterNickName",qhUser.getInviterNickName());
                jsonObj.put("inviterName",qhUser.getInviterName());
                jsonObj.put("nickName",qhUser.getNickName());
                jsonObj.put("name",qhUser.getName());
                jsonObj.put("leav",qhUser.getLeav());
                jsonObj.put("mobile",qhUser.getMobile());
                jsonObj.put("sex",qhUser.getSex());
                SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
                String dateString="";
                if(qhUser.getBirthday()!=null&&!qhUser.getBirthday().equals("")){
                    dateString = formatter.format(qhUser.getBirthday());
                }
                jsonObj.put("birthday",dateString);
                jsonObj.put("idCard",qhUser.getIdCard());
                jsonObj.put("alipayName",qhUser.getAlipayName());
                jsonObj.put("alipay", qhUser.getAlipay());
                jsonObj.put("wechat",qhUser.getWechat());
                jsonObj.put("totalFund",qhUser.getTotalFund());
                jsonObj.put("availFund",qhUser.getAvailFund());
                jsonObj.put("blockFund",qhUser.getBlockFund());
                jsonObj.put("isNew",qhUser.getIsNew());
                jsonObj.put("isRealName",qhUser.getIsRealName());
                jsonObj.put("accessToken",qhUser.getAccessToken());
                jsonObj.put("createUser",qhUser.getCreateUser());
                jsonObj.put("createDateTime",qhUser.getCreateDateTime());
                jsonObj.put("modifyUser",qhUser.getModifyUser());
                jsonObj.put("modifyDateTime",qhUser.getModifyDateTime());
                jsonObj.put("state", qhUser.getState());

                resultMap.put("state", "1");
                resultMap.put("msg", "登陆成功");
                resultMap.put("data", jsonObj);

                //操作日志
                Operlog(0, "登陆", "login", "", mobile, 0, "", request);

                String inviter = qhUser.getInviter();
                i = 0;
                intParamMap.clear();
                intParamMap.put(i++, inviter);
                if(qhUser.getIsLogin()==0) {
                    hql = " from QhUser where inviter=? and islogin=1";
                    List<QhUser> inviterCnt = dbServices.findListByHql(hql, intParamMap);
                    qhUser.setInviterIndex(inviterCnt.size() + 1);
                    qhUser.setSrcPla(srcpla);
                }

                hql="from QhUser where mobile=?";
                List<QhUser> qhUsers=dbServices.findListByHql(hql,intParamMap);
                if(qhUsers.size()>0) {
                    //如果是属于某个人第一次邀请的徒弟，则把是否正式徒弟设为true
                    if(qhUser.getInviterIndex()==1) {
                        qhUser.setIsApprentice(1);
                        dbServices.update(qhUser);

                        if(qhUser.getIsLogin()==0) {
                            //添加新手任务的首次邀请
                            addUserTaskCentre(qhUsers.get(0).getId(), 3, "首次邀请", 0);

                            //添加邀请人收益记录
                            addProfit(qhUsers.get(0).getId(), 0, "邀请", qhSysPar.getInviteCash(), qhUsers.get(0).getMobile(), mobile);

                            //添加新手任务的限时收徒
                            TimeLit_Invite(qhUsers.get(0).getId());
                        }
                    }
                    //升级
                    upgrade(qhUsers.get(0).getId());
                }
                qhUser.setIsLogin(1);
                dbServices.update(qhUser);

                if (qhUser.getIsNew() == 1) {
                    i = 0;
                    intParamMap.clear();
                    intParamMap.put(i++, inviter);
                    hql = "from QhUserNoviceTask where createuser=? and type='邀请' and state=99";
                    List<QhUserNoviceTask> qhUserNoviceTasks = dbServices.findListByHql(hql, intParamMap);
                    if (qhUserNoviceTasks.size() > 0) {
                        QhUserNoviceTask qhUserNoviceTask = dbServices.getEntity(QhUserNoviceTask.class, qhUserNoviceTasks.get(0).getId());
                        qhUserNoviceTask.setState(1);
                        dbServices.update(qhUserNoviceTask);
                        //发送“可以提现”通告
                        sendTaskCompleteMsg(qhUsers.get(0).getId(), inviter);
                    }
                }
            }
        }
        else{
            PubRegister("", "", tempContextUrl+"static/images/headimg.png", mobile, "", "", "", "", "", "",request);
            //resultMap.put("state", "0");
            //resultMap.put("msg", "登陆失败，用户名错误");
            //resultMap.put("data", new HashMap());
        }
    }
    //退出登陆
    public void logout(HttpServletRequest request,HttpServletResponse response) throws Exception {
        JSONObject param = CodeHelper.getRequestParams(request);
        Integer uid=param.getInteger("uid");
        QhUser qhUser=dbServices.getEntity(QhUser.class, uid);
        qhUser.setAccessToken("");
        dbServices.update(qhUser);
        resultMap.put("state", "1");
        resultMap.put("msg", "退出登陆成功");
        resultMap.put("data", qhUser);
    }
    //注册
    public void register(HttpServletRequest request,HttpServletResponse response) throws Exception {
        JSONObject param = CodeHelper.getRequestParams(request);
        StringBuffer url = request.getRequestURL();
        String tempContextUrl = url.delete(url.length() - request.getRequestURI().length(), url.length()).append(request.getContextPath()).append("/").toString();

        String code=param.getString("code");
        String inviter=param.getString("inviter");
        String mobile=param.getString("mobile");
        String picUrl=tempContextUrl+"static/images/headimg.png";
        String pwd=param.getString("pwd");

        if(inviter==null||inviter.equals("")) {
            i = 0;
            intParamMap.clear();
            intParamMap.put(i++, mobile);
            intParamMap.put(i++, code);
            hql = " from QhCode where mobile=? and code=?";
            List<QhCode> qhCodes = dbServices.findListByHql(hql, intParamMap);
            if (qhCodes.size() == 0) {
                resultMap.put("state", "0");
                resultMap.put("msg", "验证码不正确");
                resultMap.put("data", new HashMap());
                return;
            }
        }

        QhSysPar qhSysPar=dbServices.getEntity(QhSysPar.class, 1);

        hql = " from QhUser where mobile=? ";
        i=0;
        intParamMap.clear();
        intParamMap.put(i++, mobile);
        List<QhUser> regTutor = dbServices.findListByHql(hql,intParamMap);
        if(regTutor.size()>0){
            resultMap.put("state", "0");
            resultMap.put("msg", "该手机号已注册");
            resultMap.put("data", new HashMap());
        }
        else{
            PubRegister(inviter, "", picUrl, mobile, pwd, "", "", "", "", "",request);
        }
    }
    //获取个人信息
    public void getUserInfo(HttpServletRequest request,HttpServletResponse response) throws Exception {
        JSONObject param = CodeHelper.getRequestParams(request);
        QhUser qhUser=dbServices.getEntity(QhUser.class, param.getInteger("uid"));
        resultMap.put("state", "1");
        resultMap.put("msg", "");
        resultMap.put("data", qhUser);
    }
    public void isRegister(HttpServletRequest request,HttpServletResponse response) throws Exception {
        JSONObject param = CodeHelper.getRequestParams(request);
        String openid=param.getString("openid");
        System.out.println("陈威平openid：" + openid);
        hql = " from QhUser where openid=? ";
        i=0;
        intParamMap.clear();
        intParamMap.put(i++, openid);
        List<QhUser> qhUsers = dbServices.findListByHql(hql,intParamMap);
        if(qhUsers.size()>0){
            resultMap.put("state", "1");
            resultMap.put("msg", "已注册");
            resultMap.put("data", qhUsers.get(0));
        }
        else{
            resultMap.put("state", "0");
            resultMap.put("msg", "未注册");
            resultMap.put("data", new HashMap());
        }
        System.out.println(resultMap);
    }
    public void wechatLogin(HttpServletRequest request,HttpServletResponse response) throws Exception {
        System.out.println("陈威平：wechatlogin");
        JSONObject param = CodeHelper.getRequestParams(request);
        String openid=param.getString("openid");
        String pwd=param.getString("pwd");
        String nickname=param.getString("nickname");
        String sex=param.getString("sex");
        String country=param.getString("country");
        String province=param.getString("province");
        String city=param.getString("city");
        String headimgurl=param.getString("headimgurl");
        String mobile=param.getString("mobile");
        String inviter=param.getString("inviter");
        String code=param.getString("code");

        i = 0;
        intParamMap.clear();
        intParamMap.put(i++, mobile);
        intParamMap.put(i++, code);
        hql = " from QhCode where mobile=? and code=?";
        List<QhCode> qhCodes = dbServices.findListByHql(hql, intParamMap);
        if (qhCodes.size() == 0) {
            resultMap.put("state", "0");
            resultMap.put("msg", "验证码不正确");
            resultMap.put("data", new HashMap());
            return;
        }

        hql = " from QhUser where mobile=? ";
        i=0;
        intParamMap.clear();
        intParamMap.put(i++, mobile);
        List<QhUser> qhUsers = dbServices.findListByHql(hql,intParamMap);
        if(qhUsers.size()>0){
            QhUser qhUser=dbServices.getEntity(QhUser.class,qhUsers.get(0).getId());

            if(qhUser.getOpenId()!=null&&!qhUser.getOpenId().equals("")) {
                hql = " from QhUser where openid=? and mobile=? ";
                i = 0;
                intParamMap.clear();
                intParamMap.put(i++, openid);
                intParamMap.put(i++, mobile);
                qhUsers = dbServices.findListByHql(hql, intParamMap);
                if (qhUsers.size() == 0) {
                    resultMap.put("state", "0");
                    resultMap.put("msg", "该手机已经绑定过微信");
                    resultMap.put("data", qhUser);

                    return;
                }
            }

            qhUser.setOpenId(openid);
            qhUser.setPwd(EncryptUtils.encrypt(pwd));
            qhUser.setPicUrl(headimgurl);
            qhUser.setNickName(nickname);
            qhUser.setSex(sex);
            qhUser.setCountry(country);
            qhUser.setProvince(province);
            qhUser.setCity(city);

            i=0;
            intParamMap.clear();
            intParamMap.put(i++, qhUser.getInviter());
            if(qhUser.getIsLogin()==0) {
                hql = " from QhUser where inviter=? and islogin=1";
                List<QhUser> inviterCnt = dbServices.findListByHql(hql, intParamMap);
                qhUser.setInviterIndex(inviterCnt.size() + 1);

                qhUser.setSrcPla(param.getString("srcpla"));
            }

            qhUser.setIsLogin(1);
            dbServices.update(qhUser);
            resultMap.put("state", "1");
            resultMap.put("msg", "登陆成功");
            resultMap.put("data", qhUser);

            if (qhUser.getIsNew() == 1) {
                i = 0;
                intParamMap.clear();
                intParamMap.put(i++, qhUsers.get(0).getMobile());
                hql = "from QhUserNoviceTask where createuser=? and type='邀请' and state=99";
                List<QhUserNoviceTask> qhUserNoviceTasks = dbServices.findListByHql(hql, intParamMap);
                if (qhUserNoviceTasks.size() > 0) {
                    QhUserNoviceTask qhUserNoviceTask = dbServices.getEntity(QhUserNoviceTask.class, qhUserNoviceTasks.get(0).getId());
                    qhUserNoviceTask.setState(1);
                    dbServices.update(qhUserNoviceTask);
                    //发送“可以提现”通告
                    sendTaskCompleteMsg(qhUsers.get(0).getId(), qhUsers.get(0).getMobile());
                }
            }

            //如果是属于某个人第一次邀请的徒弟，则把是否正式徒弟设为true
            if(qhUser.getInviterIndex()==1) {
                qhUser.setIsApprentice(1);
                dbServices.update(qhUser);
            }
            //升级
            upgrade(qhUsers.get(0).getId());

        } else {
            //////////
            PubRegister(inviter, openid, headimgurl, mobile, "123456", nickname, sex, country, province, city, request);
        }
        //操作日志
        Operlog(0, "微信登陆", "wechatLogin", "", mobile, 0,"",request);

        //新手任务微信绑定
        hql = " from QhUser where mobile=? ";
        i=0;
        intParamMap.clear();
        intParamMap.put(i++, mobile);
        qhUsers = dbServices.findListByHql(hql,intParamMap);
        if(qhUsers.size()>0){
            addUserTaskCentre(qhUsers.get(0).getId(),2,"微信绑定",0);
        }
    }
    //忘记密码
    public void forgetPwd(HttpServletRequest request,HttpServletResponse response) throws Exception {
        JSONObject param = CodeHelper.getRequestParams(request);
        String mobile=param.getString("mobile");
        i = 0;
        intParamMap.clear();
        intParamMap.put(i++, mobile);
        intParamMap.put(i++, param.getString("code"));
        hql = " from QhCode where mobile=? and code=?";
        List<QhCode> qhCodes = dbServices.findListByHql(hql, intParamMap);
        if (qhCodes.size() == 0) {
            resultMap.put("state", "0");
            resultMap.put("msg", "验证码不正确");
            resultMap.put("data", new HashMap());
            return;
        }

        i = 0;
        intParamMap.clear();
        intParamMap.put(i++, mobile);
        hql="from QhUser where mobile=?";
        List<QhUser> qhUsers = dbServices.findListByHql(hql,intParamMap);
        if(qhUsers.size()>0){
            QhUser qhUser=dbServices.getEntity(QhUser.class, qhUsers.get(0).getId());
            qhUser.setPwd(EncryptUtils.encrypt(param.getString("pwd")));
            dbServices.update(qhUser);

            resultMap.put("state", "1");
            resultMap.put("msg", "密码重置成功");
            resultMap.put("data", qhUser);
        }
    }
    //获取首页收益
    public void getInProfit(HttpServletRequest request,HttpServletResponse response) throws Exception {
        JSONObject param = CodeHelper.getRequestParams(request);
        time=new SimpleDateFormat("yyyy-MM-dd");
        nowTime=new Date();
        strnowTime= time.format(nowTime);
        Integer uid=param.getInteger("uid");
        QhUser qhUser=dbServices.getEntity(QhUser.class, uid);
        QhSysPar qhSysPar=dbServices.getEntity(QhSysPar.class, 1);
        String mobile="";
        Double AvailFund=Double.valueOf(0);

        if(qhUser!=null){
            mobile=qhUser.getMobile();
            AvailFund= qhUser.getAvailFund();
        }

        i=0;
        intParamMap.clear();
        hql="select sum(CashMoney) from qh_user_profit where CreateUser=?";
        intParamMap.put(i++, mobile);
        Object qhUserProfit= dbServices.findUniqueBySql(hql, intParamMap);
        Double ProfitUid= Double.valueOf(0);
        if(qhUserProfit!=null) {
            ProfitUid = Double.parseDouble(qhUserProfit.toString());
        }
        JSONObject jobj = new JSONObject();
        jobj.put("nowProfit", qhSysPar.getExpectProfit());
        jobj.put("ProfitUid", ProfitUid);
        jobj.put("AvailFund", AvailFund);
        resultMap.put("state", "1");
        resultMap.put("msg", "");
        resultMap.put("data", jobj);
    }
    //获取首页广告
    public void getInAdv(HttpServletRequest request,HttpServletResponse response) throws Exception {
        hql="select * from qh_adv where type='首页' order by createdatetime desc limit 0,3 ";
        List<QhAdv> qhAdvs = dbServices.findListBySql(hql, new HashMap(), QhAdv.class);
        resultMap.put("state", "1");
        resultMap.put("msg", "");
        resultMap.put("data", qhAdvs);
    }
    //获取邀请页广告
    public void getInviteAdv(HttpServletRequest request,HttpServletResponse response) throws Exception {
        hql="select * from qh_adv where type='邀请赚钱' order by createdatetime desc limit 0,2 ";
        List<QhAdv> qhAdvs = dbServices.findListBySql(hql, new HashMap(), QhAdv.class);
        resultMap.put("state", "1");
        resultMap.put("msg", "");
        resultMap.put("data", qhAdvs);
    }
    //获取任务大厅广告
    public void getTaskAdv(HttpServletRequest request,HttpServletResponse response) throws Exception {
        hql="select * from qh_adv where type='任务大厅' order by createdatetime desc limit 1 ";
        List<QhAdv> qhAdvs = dbServices.findListBySql(hql, new HashMap(), QhAdv.class);
        resultMap.put("state", "1");
        resultMap.put("msg", "");
        resultMap.put("data", qhAdvs);
    }
    //获取开机引导
    public void getGuide(HttpServletRequest request,HttpServletResponse response) throws Exception {
        hql="select * from qh_adv where type='开机引导' limit 0,3";
        List<QhAdv> qhAdvs = dbServices.findListBySql(hql, new HashMap(), QhAdv.class);
        resultMap.put("state", "1");
        resultMap.put("msg", "");
        resultMap.put("data", qhAdvs);
    }
    //获取活动公告广告
    public void getActivityAdv(HttpServletRequest request,HttpServletResponse response) throws Exception {
        hql="select * from qh_adv where type='活动公告' order by createdatetime desc limit 1 ";
        List<QhAdv> qhAdvs = dbServices.findListBySql(hql, new HashMap(), QhAdv.class);
        resultMap.put("state", "1");
        resultMap.put("msg", "");
        resultMap.put("data", qhAdvs);
    }
    //获取收益明细
    public void getProfitDetail(HttpServletRequest request,HttpServletResponse response) throws Exception {
        JSONObject param = CodeHelper.getRequestParams(request);

        StringBuffer url = request.getRequestURL();
        String tempContextUrl = url.delete(url.length() - request.getRequestURI().length(), url.length()).append(request.getContextPath()).append("/").toString();

        String mobile=param.getString("mobile");

        i=0;
        intParamMap.clear();
        intParamMap.put(i++, mobile);
        hql="select DATE_FORMAT(createdatetime,'%Y-%m-%d'),sum(CashMoney) from qh_user_profit where CreateUser=? group by DATE_FORMAT(createdatetime,'%Y-%m-%d') order by DATE_FORMAT(createdatetime,'%Y-%m-%d') desc";

        List profits = dbServices.findListBySql(hql, intParamMap);
        List<JSONObject> jsonProfits= Lists.newArrayList();
        for(int i=0;i<profits.size();i++) {
            JSONObject jsonProfit = new JSONObject();
            Object[] arr = (Object[]) profits.get(i);
            for (int z = 0; z < arr.length; z++) {
                if(z==0) {
                    jsonProfit.put("date", arr[z]);
                }
                else if(z==1){
                    jsonProfit.put("cashmoney", arr[z]);
                }
            }

            List<JSONObject> jsonProfitDetails= Lists.newArrayList();
            JSONObject jsonProfitDetail = new JSONObject();

            hql="select sum(CashMoney) from qh_user_profit where CreateUser=? and DATE_FORMAT(createdatetime,'%Y-%m-%d')='"+arr[0]+"' and (type='分享' or type='阅读' or type='首次注册')";
            Object taskProfit= dbServices.findUniqueBySql(hql, intParamMap);
            if(taskProfit==null){
                taskProfit=0;
            }
            jsonProfitDetail = new JSONObject();
            jsonProfitDetail.put("icon",1);
            jsonProfitDetail.put("type", "任务收益");
            jsonProfitDetail.put("cashmoney", Double.valueOf(taskProfit.toString()));
            jsonProfitDetails.add(jsonProfitDetail);

            hql="select sum(CashMoney) from qh_user_profit where CreateUser=? and DATE_FORMAT(createdatetime,'%Y-%m-%d')='"+arr[0]+"' and (type='邀请' or type='首次邀请')";
            taskProfit= dbServices.findUniqueBySql(hql, intParamMap);
            if(taskProfit==null){
                taskProfit=0;
            }
            jsonProfitDetail = new JSONObject();
            jsonProfitDetail.put("icon",2);
            jsonProfitDetail.put("type", "邀请收益");
            jsonProfitDetail.put("cashmoney", Double.valueOf(taskProfit.toString()));
            jsonProfitDetails.add(jsonProfitDetail);

            hql="select sum(CashMoney) from qh_user_profit where CreateUser=? and DATE_FORMAT(createdatetime,'%Y-%m-%d')='"+arr[0]+"' and type='学徒'";
            taskProfit= dbServices.findUniqueBySql(hql, intParamMap);
            if(taskProfit==null){
                taskProfit=0;
            }
            jsonProfitDetail = new JSONObject();
            jsonProfitDetail.put("icon",3);
            jsonProfitDetail.put("type", "学徒收益");
            jsonProfitDetail.put("cashmoney", Double.valueOf(taskProfit.toString()));
            jsonProfitDetails.add(jsonProfitDetail);

            hql="select sum(CashMoney) from qh_user_profit where CreateUser=? and DATE_FORMAT(createdatetime,'%Y-%m-%d')='"+arr[0]+"' and type='下载'";
            taskProfit= dbServices.findUniqueBySql(hql, intParamMap);
            if(taskProfit==null){
                taskProfit=0;
            }
            jsonProfitDetail = new JSONObject();
            jsonProfitDetail.put("icon",4);
            jsonProfitDetail.put("type", "下载收益");
            jsonProfitDetail.put("cashmoney", Double.valueOf(taskProfit.toString()));
            jsonProfitDetails.add(jsonProfitDetail);

            hql="select sum(CashMoney) from qh_user_profit where CreateUser=? and DATE_FORMAT(createdatetime,'%Y-%m-%d')='"+arr[0]+"' and type='奖励'";
            taskProfit= dbServices.findUniqueBySql(hql, intParamMap);
            if(taskProfit==null){
                taskProfit=0;
            }
            jsonProfitDetail = new JSONObject();
            jsonProfitDetail.put("icon",5);
            jsonProfitDetail.put("type", "其他收益");
            jsonProfitDetail.put("cashmoney", Double.valueOf(taskProfit.toString()));
            jsonProfitDetails.add(jsonProfitDetail);

            jsonProfit.put("data", jsonProfitDetails);

            jsonProfits.add(jsonProfit);
        }
        resultMap.put("state", "1");
        resultMap.put("msg", "");
        resultMap.put("data", jsonProfits);

    }
    public void getProfitList(HttpServletRequest request,HttpServletResponse response) throws Exception {
        Date nowTime=new Date();
        SimpleDateFormat fmttime=new SimpleDateFormat("yyyy-MM-dd HH:mm");

        JSONObject param = CodeHelper.getRequestParams(request);
        QhUser qhUser=dbServices.getEntity(QhUser.class, param.getInteger("uid"));
        String mobile=qhUser.getMobile();
        String date=param.getString("date");
        String type=param.getString("type");
        i=0;
        intParamMap.clear();
        intParamMap.put(i++, mobile);
        intParamMap.put(i++, date);
        hql="select * from qh_user_profit where CreateUser=? and DATE_FORMAT(createdatetime,'%Y-%m-%d')=?";
        if(type.equals("任务")){
            hql+=" and (type='分享' or type='阅读' or type='首次注册')";
        }else if(type.equals("其他")){
            hql+=" and type='奖励'";
        }
        else if(type.equals("邀请")){
            hql+=" and (type='邀请' or type='首次邀请')";
        }
        else{
            intParamMap.put(i++, type);
            hql+=" and type=?";
        }
        hql+=" order by createdatetime desc";
        List<QhUserProfit> qhUserProfits = dbServices.findListBySql(hql, intParamMap, QhUserProfit.class);
        List<JSONObject> jsonObjects = Lists.newArrayList();
        for(QhUserProfit qhUserProfit:qhUserProfits){
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("id",qhUserProfit.getId());
            jsonObject.put("taskId",qhUserProfit.getTaskId());
            jsonObject.put("type",qhUserProfit.getType());
            jsonObject.put("note",qhUserProfit.getNote());
            jsonObject.put("cashMoney",qhUserProfit.getCashMoney());
            jsonObject.put("createUser",qhUserProfit.getCreateUser());
            jsonObject.put("createDateTime",fmttime.format(qhUserProfit.getCreateDateTime()));
            jsonObject.put("modifyUser",qhUserProfit.getModifyUser());
            jsonObject.put("modifyDateTime",fmttime.format(qhUserProfit.getModifyDateTime()));
            jsonObject.put("state", qhUserProfit.getState());
            jsonObjects.add(jsonObject);
        }
        resultMap.put("state", "1");
        resultMap.put("msg", "");
        resultMap.put("data", jsonObjects);
    }
    //获取收入排行
    public void getRanking(HttpServletRequest request,HttpServletResponse response) throws Exception {
        hql="from QhRanking order by sort";
        StringBuffer url = request.getRequestURL();
        String tempContextUrl = url.delete(url.length() - request.getRequestURI().length(), url.length()).append(request.getContextPath()).append("/").toString();
        List<JSONObject> jobRanks = Lists.newArrayList();

        List<QhRanking> qhRankings = dbServices.findListByHql(hql, new HashMap());
        Integer i=1;
        for(QhRanking qhRanking:qhRankings){
            JSONObject jobRank = new JSONObject();
            jobRank.put("icon",i);
            jobRank.put("id",qhRanking.getId());
            jobRank.put("sort",qhRanking.getSort());
            jobRank.put("name",qhRanking.getName());
            jobRank.put("studentCnt",qhRanking.getStudentCnt());
            jobRank.put("cashMoney",qhRanking.getCashMoney());
            jobRank.put("note",qhRanking.getNote());
            jobRank.put("createUser",qhRanking.getCreateUser());
            jobRank.put("createDateTime",qhRanking.getCreateDateTime());
            jobRank.put("modifyUser",qhRanking.getModifyUser());
            jobRank.put("modifyDateTime",qhRanking.getModifyDateTime());
            jobRank.put("state",qhRanking.getState());
            jobRanks.add(jobRank);
            i++;
        }
        resultMap.put("state", "1");
        resultMap.put("msg", "");
        resultMap.put("data", jobRanks);
    }
    //新手任务
    public void getNoviceTaskList(HttpServletRequest request,HttpServletResponse response) throws Exception {
        JSONObject param = CodeHelper.getRequestParams(request);
        Integer uid=param.getInteger("uid");
        QhUser qhUser=dbServices.getEntity(QhUser.class, uid);

        StringBuffer url = request.getRequestURL();
        String tempContextUrl = url.delete(url.length() - request.getRequestURI().length(), url.length()).append(request.getContextPath()).append("/").toString();

        hql="select a.name,b.id,b.pid,b.type,b.picUrl,b.cnt,b.sigleCashMoney,b.cashMoney" +
        ",b.sort,b.effectTime,b.note,b.createUser,b.createDateTime,b.modifyUser" +
        ",b.modifyDateTime,b.state from qh_novice_task a inner join qh_user_novice_task b on a.id=b.pid where b.createuser=? order by a.sort";
        i=0;
        intParamMap.clear();
        intParamMap.put(i++, qhUser.getMobile());

        List<QhUserNoviceTask> qhUserNoviceTasks = dbServices.findListBySql(hql, intParamMap, QhUserNoviceTask.class);
        List<JSONObject> jsonObjects= Lists.newArrayList();
        for(QhUserNoviceTask qhUserNoviceTask:qhUserNoviceTasks) {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("id", qhUserNoviceTask.getId());
            jsonObject.put("type", qhUserNoviceTask.getType());
            jsonObject.put("name", qhUserNoviceTask.getName());
            jsonObject.put("note", qhUserNoviceTask.getNote());
            if(qhUserNoviceTask.getType().equals("分享")){
                hql="select count(0) from qh_user_profit where createuser=? and type='分享'";
                Object Ocnt=dbServices.findUniqueBySql(hql,intParamMap);
                Integer cnt=qhUserNoviceTask.getCnt()-Integer.valueOf(Ocnt.toString());
                if(cnt>0){
                    jsonObject.put("name", qhUserNoviceTask.getName()+"（还有"+cnt+"条未分享）");
                    jsonObject.put("note", qhUserNoviceTask.getNote()+"（还有"+cnt+"条未分享）");
                }
            }
            jsonObject.put("picUrl", qhUserNoviceTask.getState()==1?"1":"2");
            jsonObject.put("cnt", qhUserNoviceTask.getCnt());
            jsonObject.put("sigleCashMoney", qhUserNoviceTask.getSigleCashMoney());
            jsonObject.put("cashMoney", qhUserNoviceTask.getCashMoney());
            jsonObject.put("sort", qhUserNoviceTask.getSort());
            jsonObject.put("effectTime", time.format(qhUserNoviceTask.getEffectTime()));
            jsonObject.put("createUser", qhUserNoviceTask.getCreateUser());
            jsonObject.put("createDateTime", time.format(qhUserNoviceTask.getCreateDateTime()));
            jsonObject.put("modifyUser", qhUserNoviceTask.getModifyUser());
            jsonObject.put("modifyDateTime", time.format(qhUserNoviceTask.getModifyDateTime()));
            jsonObject.put("state", qhUserNoviceTask.getState());
            jsonObjects.add(jsonObject);
        }
        resultMap.put("state", "1");
        resultMap.put("msg", "");
        resultMap.put("data", jsonObjects);
    }

    public void getNoviceTaskByInviter(HttpServletRequest request,HttpServletResponse response) throws Exception {
        QhSysPar qhSysPar=dbServices.getEntity(QhSysPar.class, 1);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("InviteNews",qhSysPar.getInviteNews());
        resultMap.put("state", "1");
        resultMap.put("msg", "");
        resultMap.put("data", jsonObject);
    }
    //获取首页任务列表
    public void getIndexTaskList(HttpServletRequest request,HttpServletResponse response) throws Exception {
        Date nowTime=new Date();
        SimpleDateFormat fmttime=new SimpleDateFormat("yyyy-MM-dd");
        QhSysPar qhSysPar=dbServices.getEntity(QhSysPar.class, 1);

        JSONObject param = CodeHelper.getRequestParams(request);
        Integer uid=param.getInteger("uid");
        String mobile="";

        hql="select id,Source,Type,Category,Name,Title,StartDate,EndDate,"
                +"TimeType,StartTime,EndTime,Adver,AdvLink,PicUrl,AdvCopy," +
                "Commission as InvestAmount,AdvPrice,Commission,RealCommission," +
                "InvestPosition,RewardAmount,ShareNum,ShareProfit,ReadProfit," +
                "DownProfit,CashProfit,InviteProfit,ShareDirect,PayMode," +
                "ShareUrl,Sort,Note,CreateUser,CreateDateTime,ModifyUser," +
                "ModifyDateTime,State,EffectTime from qh_task where state=1";
        i=0;
        intParamMap.clear();
        intParamMap.put(i++, qhSysPar.getTaskShowCnt());

        QhUser qhUser=dbServices.getEntity(QhUser.class, uid);
        /*if(qhUser!=null){
            mobile=qhUser.getMobile();
            intParamMap.put(i++, mobile);
            intParamMap.put(i++, qhSysPar.getTaskShowCnt());
            hql+=" and id not in(select ifnull(taskid,0) from qh_task_share where CreateUser=? and iscontinue=0)";
        }
        else{
            intParamMap.put(i++, qhSysPar.getTaskShowCnt());
        }

        String sql = "from QhTaskShare where CreateUser='"+qhUser.getMobile()+"' and DATE_FORMAT(CreateDateTime,'%Y-%m-%d')=DATE_FORMAT(now(),'%Y-%m-%d')";
        List<QhTaskShare> qhTaskShares = dbServices.findListByHql(sql, new HashMap());
        if (qhTaskShares.size() == qhSysPar.getTaskShareCnt()) {
            hql+=" and 1!=1";
        }*/

        hql+=" order by ModifyDateTime limit ?";
        List<QhTask> qhTasks = dbServices.findListBySql(hql, intParamMap, QhTask.class);

        resultMap.put("state", "1");
        resultMap.put("msg", "");
        resultMap.put("data", qhTasks);

        if(qhUser!=null) {
            i = 0;
            intParamMap.clear();
            intParamMap.put(i++, mobile);
            intParamMap.put(i++, fmttime.format(nowTime));
            intParamMap.put(i++, param.getString("srcpla"));
            hql = "from QhUserOperLog where CreateUser=? and date_format(createdatetime,'%Y-%m-%d')=? and srcpla=? and FunName='活跃'";
            List<QhUserOperLog> qhUserOperLogs = dbServices.findListByHql(hql, intParamMap);
            if (qhUserOperLogs.size() == 0) {
                Operlog(0, "活跃", "getIndexTaskList", "", qhUser.getMobile(), 0, "", request);
            }
        }
    }
    //获取奖励任务列表
    public void getRewardTaskList(HttpServletRequest request,HttpServletResponse response) throws Exception {
        JSONObject param = CodeHelper.getRequestParams(request);
        Integer uid=param.getInteger("uid");
        QhUser qhUser=dbServices.getEntity(QhUser.class,uid);
        i=0;
        intParamMap.clear();
        intParamMap.put(i++, qhUser.getMobile());
        hql="select b.id,b.Pid,a.Title,a.Type,a.RewardType,a.Number,"+
            "a.CashMoney,b.CreateUser,b.CreateDateTime,b.ModifyUser,"+
            "b.ModifyDateTime,b.State from qh_tool_cash a inner join "+
            "qh_user_tool_cash b on a.id=b.pid where b.createuser=? ";
        List<QhUserToolCash> qhUserToolCashs = dbServices.findListBySql(hql, intParamMap, QhUserToolCash.class);

        resultMap.put("state", "1");
        resultMap.put("msg", "");
        resultMap.put("data", qhUserToolCashs);

    }
    //分享任务
    public void getTaskList(HttpServletRequest request,HttpServletResponse response) throws Exception {
        QhSysPar qhSysPar=dbServices.getEntity(QhSysPar.class, 1);

        JSONObject param = CodeHelper.getRequestParams(request);
        Integer uid=param.getInteger("uid");

        QhUser qhUser=dbServices.getEntity(QhUser.class, uid);

        String mobile=qhUser.getMobile();
        CurrPage = param.containsKey("page") ? Integer.parseInt(param.getString("page")):1;
        RdCount = 0;
        PageCount = 0;
        i=0;
        intParamMap.clear();

        hql="select id,Source,Type,Category,Name,Title,StartDate,EndDate,"
        +"TimeType,StartTime,EndTime,Adver,AdvLink,PicUrl,AdvCopy," +
        "Commission as InvestAmount,AdvPrice,Commission,RealCommission," +
        "InvestPosition,RewardAmount,ShareNum,ShareProfit,ReadProfit," +
        "DownProfit,CashProfit,InviteProfit,ShareDirect,PayMode," +
        "ShareUrl,Sort,Note,CreateUser,CreateDateTime,ModifyUser," +
        "ModifyDateTime,State,EffectTime from qh_task where state=1";
        //hql+=" and id not in(select ifnull(taskid,0) from qh_task_share where CreateUser=? and iscontinue=0)";

        /*String sql = "from QhTaskShare where CreateUser=? and DATE_FORMAT(CreateDateTime,'%Y-%m-%d')=DATE_FORMAT(now(),'%Y-%m-%d')";
        List<QhTaskShare> qhTaskShares = dbServices.findListByHql(sql, intParamMap);
        if (qhTaskShares.size() == qhSysPar.getTaskShareCnt()) {
            hql+=" and 1!=1";
        }*/

        List<QhTask> qhTasks = dbServices.findListBySql(hql, new HashMap());

        RdCount=qhTasks.size();
        PageCount = Getpagecount(RdCount, PageSize);
        sid = Getstartpageindex(PageSize, CurrPage);
        eid = PageSize;

        intParamMap.put(i++, sid);
        intParamMap.put(i++, eid);
        hql+=" order by ModifyDateTime limit ?,?";
        qhTasks = dbServices.findListBySql(hql, intParamMap, QhTask.class);
        resultMap.put("state", "1");
        resultMap.put("msg", "");
        resultMap.put("data", qhTasks);

    }

    //分享进行中任务
    public void getTaskIngList(HttpServletRequest request,HttpServletResponse response) throws Exception {
        JSONObject param = CodeHelper.getRequestParams(request);

        Integer uid=param.getInteger("uid");
        String mobile=param.getString("mobile");
        Integer state=param.getInteger("state");
        CurrPage = param.containsKey("page") ? Integer.parseInt(param.getString("page")):1;
        RdCount = 0;
        PageCount = 0;
        i=0;
        intParamMap.clear();
        hql="select a.id,a.Title,a.AdvLink,a.ShareDirect,a.ReadProfit,ifnull(a.Commission,0) as Commission,a.PicUrl,a.ShareUrl,a.InvestAmount,a.AdvCopy"
        +",a.Note,a.State,ifnull((select sum(CashMoney) from qh_user_profit where TaskId=a.id and CreateUser=? and type='分享'),0)+ifnull((select sum(CashMoney) from qh_user_profit where TaskId=a.id and type='阅读' and CreateUser=?),0) as ShareProfit"
        +",(select count(0) from qh_user_profit where TaskId=a.id and CreateUser=? and type='阅读')ReadNum from qh_task a inner join"
        +" qh_task_share b on a.id=b.taskid where a.state=1 and b.CreateUser=? and b.iscontinue=0";
        intParamMap.put(i++, mobile);
        intParamMap.put(i++, mobile);
        intParamMap.put(i++, mobile);
        intParamMap.put(i++, mobile);
        if(state!=null&&state==-1){
            intParamMap.put(i++, state);
            hql+=" and a.state=?";
        }
        List<QhTaskIng> qhTasks = dbServices.findListBySql(hql, intParamMap);
        resultMap.put("cnt", qhTasks.size());

        RdCount=qhTasks.size();
        PageCount = Getpagecount(RdCount, PageSize);
        sid = Getstartpageindex(PageSize, CurrPage);
        eid = PageSize;

        intParamMap.put(i++, sid);
        intParamMap.put(i++, eid);
        hql+=" order by b.CreateDateTime desc limit ?,?";
        qhTasks = dbServices.findListBySql(hql, intParamMap, QhTaskIng.class);
        resultMap.put("state", "1");
        resultMap.put("msg", "");

        resultMap.put("data", qhTasks);

    }
    //提现
    public void getCash(HttpServletRequest request,HttpServletResponse response) throws Exception {
        JSONObject param = CodeHelper.getRequestParams(request);
        Integer uid=param.getInteger("uid");
        String type=param.getString("type");
        String collection=param.getString("collection");
        String cashType=param.getString("cashType");
        Double cashmoney=param.getDouble("cashmoney");

        if(!ToolController.isInteger(param.getString("cashmoney"))){
            resultMap.put("state", "0");
            resultMap.put("msg", "提现金额必须是整数");
            resultMap.put("data", new HashMap());
            return;
        }
        /*if(type==null){
            resultMap.put("state", "0");
            resultMap.put("msg", "缺少type（提现条件）参数");
            resultMap.put("data", new HashMap());
            return;
        }*/
        if(collection==null){
            resultMap.put("state", "0");
            resultMap.put("msg", "缺少collection（收款账户）参数");
            resultMap.put("data", new HashMap());
            return;
        }
        if(cashmoney==null){
            resultMap.put("state", "0");
            resultMap.put("msg", "缺少cashmoney（提现金额）参数");
            resultMap.put("data", new HashMap());
            return;
        }

        QhUser qhUser=dbServices.getEntity(QhUser.class, uid);
        QhSysPar qhSysPar=dbServices.getEntity(QhSysPar.class, 1);

        hql="from QhUserCash where CreateUser=?";
        i=0;
        intParamMap.clear();
        intParamMap.put(i++, qhUser.getMobile());
        List<QhUserCash> qhUserCashs = dbServices.findListByHql(hql, intParamMap);

        /*if(qhUser.getIsRealName()==0){
            resultMap.put("state", "0");
            resultMap.put("msg", "提现操作需要实名认证");
            resultMap.put("data", new HashMap());
            return;
        }*/
        if((qhUser.getAlipay()==null||qhUser.getAlipay().equals(""))&&(qhUser.getWechat()==null||qhUser.getWechat().equals(""))){
            resultMap.put("state", "0");
            resultMap.put("msg", "请设置提现账户");
            resultMap.put("data", new HashMap());
            return;
        }

        hql="select b.* from qh_novice_task a inner join qh_user_novice_task b on a.id=b.pid where b.CreateUser=? and b.state=99";
        List<QhUserNoviceTask> qhUserNoviceTasks = dbServices.findListBySql(hql, intParamMap, QhUserNoviceTask.class);
        if(qhUserNoviceTasks.size()>0){
            resultMap.put("state", "0");
            resultMap.put("msg", "新手任务还没完成");
            resultMap.put("data", new HashMap());
            return;
        }
        /*hql="select a.* from qh_task_share a inner join qh_user b on a.createuser=b.mobile where b.Inviter=?";
        List<QhTaskShare> qhTaskShareLogs = dbServices.findListBySql(hql, intParamMap);
        if(qhTaskShareLogs.size()==0){
            resultMap.put("state", "0");
            resultMap.put("msg", "还未完成新手任务-邀请徒弟");
            resultMap.put("data", new HashMap());
            return;
        }*/

        if(cashmoney>qhUser.getAvailFund()){
            resultMap.put("state", "0");
            resultMap.put("msg", "提现金额不能大于余额");
            resultMap.put("data", new HashMap());
            return;
        }

        //判断是否第一次提现
        if(qhUserCashs.size()>0){
            /*if(type.equals("工具提现")){
                hql="select a.* from qh_tool_cash a inner join qh_tool_cash_user b "
                  +"on a.id=b.pid where datediff(now(),b.CreateDateTime)<=a.ValidTerm "
                  +"and b.CreateUser=? and b.state=99";
                List<QhToolCash> qhToolCashs = dbServices.findListBySql(hql, intParamMap,QhToolCash.class);
                if(qhToolCashs.size()>0){
                    if(qhToolCashs.get(0).getCashMoney()<cashmoney){
                        cashmoney=qhToolCashs.get(0).getCashMoney();
                    }
                    QhToolCashUser qhToolCashUser=dbServices.getEntity(QhToolCashUser.class,qhToolCashs.get(0).getId());
                    qhToolCashUser.setState(1);
                    dbServices.update(qhToolCashUser);
                }
                else{
                    resultMap.put("state", "0");
                    resultMap.put("msg", "没有工具提现的提现额度");
                    resultMap.put("data", new HashMap());
                    return;
                }
            }
            else {
                if (qhUser.getAvailFund() < qhSysPar.getReflect()) {
                    resultMap.put("state", "0");
                    resultMap.put("msg", "余额不足满"+qhSysPar.getReflect()+"元提现");
                    resultMap.put("data", new HashMap());
                    return;
                }

            }*/
            if(cashmoney<1||cashmoney==1||cashmoney%10!=0){
                resultMap.put("state", "0");
                resultMap.put("msg", "提现金额应该为满元");
                resultMap.put("data", new HashMap());
                return;
            }
        }
        String msg = "";
        System.out.println(cashType);
        if(cashType.equals("微信")) {
            i = 0;
            intParamMap.clear();
            intParamMap.put(i++, qhUser.getMobile());
            hql = "select a.openid from qh_wx_user a inner join qh_user b on a.rancode=b.Wechat where b.mobile=?";
            Object openid = dbServices.findUniqueBySql(hql, intParamMap);

            SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
            String dftime = df.format(new Date());
            Random ne = new Random();
            int guid = ne.nextInt(9999 - 1000 + 1) + 1000;

            Map<String, String> parm = new HashMap<String, String>();
            parm.put("mch_appid", Constants.appId); //公众账号appid
            parm.put("mchid", Constants.partner); //商户号
            parm.put("nonce_str", UUID.randomUUID().toString().substring(0, 30)); //随机字符串
            parm.put("partner_trade_no", Constants.partner + dftime + guid); //商户订单号
            parm.put("openid", openid.toString()); //用户openid
            parm.put("check_name", "NO_CHECK"); //校验用户姓名选项 OPTION_CHECK
            //parm.put("re_user_name", "安迪"); //check_name设置为FORCE_CHECK或OPTION_CHECK，则必填
            parm.put("amount", cashmoney.intValue() * 100 + ""); //转账金额
            parm.put("desc", "提现转账"); //企业付款描述信息
            parm.put("spbill_create_ip", PayUtil.getLocalIp(request)); //Ip地址
            parm.put("sign", PayUtil.getSign(parm, Constants.partnerkey));
            System.out.println(parm);

            String restxml = HttpUtils.posts("https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers", XmlUtil.xmlFormat(parm, false));
            Map<String, String> restmap = XmlUtil.xmlParse(restxml);

            if (restmap.get("err_code") != null && restmap.get("err_code").equals("NOTENOUGH")) {
                resultMap.put("state", "0");
                resultMap.put("msg", "微信商户平台余额不足");
                msg = "微信商户平台余额不足，" + strnowTime + "提现" + cashmoney + "元失败";
                return;

            } else if (restmap.get("err_code") != null && restmap.get("err_code").equals("V2_ACCOUNT_SIMPLE_BAN")) {
                resultMap.put("state", "0");
                resultMap.put("msg", "微信账户没有实名认证");
                msg = "因为您的微信未进行实名认证，" + strnowTime + "提现" + cashmoney + "元失败，请尽快完成微信实名认证";
                return;
            }
        }
        List<JSONObject> jsonQhUserCash = Lists.newArrayList();
        JSONObject jobQhUserCash = new JSONObject();
        jobQhUserCash.put("code", ToolController.GetOrderId());
        jobQhUserCash.put("cashType", cashType);
        jobQhUserCash.put("collection", collection);
        jobQhUserCash.put("cashMoney", cashmoney);
        jobQhUserCash.put("feeMoney", 0);
        jobQhUserCash.put("cashCodition", type);
        jobQhUserCash.put("createUser", qhUser.getMobile());
        jobQhUserCash.put("createDateTime", strnowTime);
        jobQhUserCash.put("modifyUser", qhUser.getMobile());
        jobQhUserCash.put("modifyDateTime", strnowTime);
        jobQhUserCash.put("state", cashType.equals("微信")?1:99);
        jsonQhUserCash.add(jobQhUserCash);
        returnId = dbServices.saveOrupdateRecords("QhUserCash", jsonQhUserCash, null, false);
        resultMap.put("state", "1");
        resultMap.put("msg", "提现成功");
        resultMap.put("data", jsonQhUserCash);

        //扣除账户余额
        //double TotalFund=qhUser.getTotalFund() - cashmoney;
        double AvailFund = qhUser.getAvailFund() - cashmoney;
        //qhUser.setTotalFund(TotalFund);
        qhUser.setAvailFund(AvailFund);
        if (qhUser.getIsNew() == 1) {
            //设置为不是新手
            qhUser.setIsNew(0);
        }
        dbServices.update(qhUser);

        msg=strnowTime + "提现" + cashmoney+ "元成功";

        //操作日志
        Operlog(Integer.parseInt(returnId), "提交", "getCash", "", qhUser.getMobile(), 0, "", request);
        Operlog(Integer.parseInt(returnId), "系统处理中", "getCash", "", qhUser.getMobile(), 0, "", request);
        ToolController.sendMsg(msg, qhUser.getMobile());

        List<JSONObject> notices = Lists.newArrayList();
        JSONObject jobnotice = new JSONObject();
        jobnotice.put("uid",uid);
        jobnotice.put("userType", "a");
        jobnotice.put("type", "系统消息");
        jobnotice.put("title", msg);
        jobnotice.put("note", msg);
        jobnotice.put("createUser", 0);
        jobnotice.put("createDateTime", strnowTime);
        jobnotice.put("modifyUser", 0);
        jobnotice.put("modifyDateTime", strnowTime);
        jobnotice.put("state", 99);
        notices.add(jobnotice);
        dbServices.saveOrupdateRecords("QhNotice", notices, null, false);

        //添加新手任务的首次提现
        addUserTaskCentre(uid, 5, "首次提现",0);
    }
    //获取提现记录
    public void getCashList(HttpServletRequest request,HttpServletResponse response) throws Exception {
        JSONObject param = CodeHelper.getRequestParams(request);
        Integer uid=param.getInteger("uid");
        String mobile=param.getString("mobile");
        CurrPage = param.containsKey("page") ? Integer.parseInt(param.getString("page")):1;
        RdCount = 0;
        PageCount = 0;
        hql="select * from qh_user_cash where CreateUser=?";
        i=0;
        intParamMap.clear();
        intParamMap.put(i++, mobile);
        List<QhUserCash> qhUserCashs = dbServices.findListBySql(hql, intParamMap);

        RdCount=qhUserCashs.size();
        PageCount = Getpagecount(RdCount, PageSize);
        sid = Getstartpageindex(PageSize, CurrPage);
        eid = PageSize;

        intParamMap.put(i++, sid);
        intParamMap.put(i++, eid);
        hql+=" order by CreateDateTime desc limit ?,?";
        qhUserCashs = dbServices.findListBySql(hql, intParamMap, QhUserCash.class);
        resultMap.put("state", "1");
        resultMap.put("msg", "");
        resultMap.put("data", qhUserCashs);

    }
    //获取提现详情
    public void getCashView(HttpServletRequest request,HttpServletResponse response) throws Exception {
        JSONObject param = CodeHelper.getRequestParams(request);
        String mobile=param.getString("mobile");
        Integer id=param.getInteger("id");
        QhUserCash qhUserCash=dbServices.getEntity(QhUserCash.class, id);
        JSONObject jsonObject = new JSONObject();
        JSONObject jsonCash = new JSONObject();
        jsonCash.put("id",qhUserCash.getId());
        jsonCash.put("code",qhUserCash.getCode());
        jsonCash.put("collection",qhUserCash.getCollection());
        jsonCash.put("cashMoney",qhUserCash.getCashMoney());
        jsonCash.put("feeMoney", qhUserCash.getFeeMoney());
        jsonCash.put("cashCodition",qhUserCash.getCashCodition());
        jsonCash.put("note",qhUserCash.getCode());
        jsonCash.put("createUser",qhUserCash.getCreateUser());
        jsonCash.put("createDateTime",time.format(qhUserCash.getCreateDateTime()));
        jsonCash.put("modifyUser",qhUserCash.getModifyUser());
        jsonCash.put("modifyDateTime", time.format(qhUserCash.getModifyDateTime()));
        jsonCash.put("state",qhUserCash.getState()==99?"未审核":"已审核");
        jsonObject.put("cashModel", jsonCash);
        hql="from QhUserOperLog where Oid=?";
        i=0;
        intParamMap.clear();
        intParamMap.put(i++, id);
        List<QhUserOperLog> qhUserOperLogs = dbServices.findListByHql(hql, intParamMap);
        List<JSONObject> operLogs =Lists.newArrayList();
        for(QhUserOperLog qhUserOperLog:qhUserOperLogs) {
            JSONObject jsonLog = new JSONObject();
            jsonLog.put("id", qhUserOperLogs.get(0).getId());
            jsonLog.put("oid", qhUserOperLogs.get(0).getOid());
            jsonLog.put("funName", qhUserOperLogs.get(0).getFunName());
            jsonLog.put("funPar", qhUserOperLogs.get(0).getFunPar());
            jsonLog.put("optName", qhUserOperLogs.get(0).getOptName());
            jsonLog.put("srcIp", qhUserOperLogs.get(0).getSrcIp());
            jsonLog.put("address", qhUserOperLogs.get(0).getAddress());
            jsonLog.put("note", qhUserOperLogs.get(0).getNote());
            jsonLog.put("createUser", qhUserOperLogs.get(0).getCreateUser());
            jsonLog.put("createDateTime", time.format(qhUserOperLogs.get(0).getCreateDateTime()));
            jsonLog.put("modifyUser", qhUserOperLogs.get(0).getModifyUser());
            jsonLog.put("modifyDateTime", time.format(qhUserOperLogs.get(0).getModifyDateTime()));
            jsonLog.put("state",qhUserOperLogs.get(0).getState());
            operLogs.add(jsonLog);
        }
        jsonObject.put("logList", operLogs);
        resultMap.put("state", "1");
        resultMap.put("msg", "");
        resultMap.put("data", jsonObject);

    }

    //新手任务分享
    public void shareNoviceTask(HttpServletRequest request,HttpServletResponse response) throws Exception {
        QhSysPar qhSysPar=dbServices.getEntity(QhSysPar.class, 1);
        JSONObject param = CodeHelper.getRequestParams(request);
        Integer uid=param.getInteger("uid");
        String mobile=param.getString("mobile");
    }

    //分享
    public void share(HttpServletRequest request,HttpServletResponse response) throws Exception {
        System.out.println("是否调用了分享接口");
        JSONObject param = CodeHelper.getRequestParams(request);
        String type=param.getString("type");
        if(type!=null&&type.equals("邀请")){
            resultMap.put("state", "0");
            resultMap.put("msg", "");
            resultMap.put("data", new HashMap());
            return;
        }
        Integer taskid=param.getInteger("taskid");
        Integer uid=param.getInteger("uid");
        String mobile=param.getString("mobile");
        String from=param.getString("from");

        //判断是否分享过
        i = 0;
        intParamMap.clear();
        intParamMap.put(i++, mobile);
        intParamMap.put(i++, taskid);
        hql = "from QhTaskShare where CreateUser=? and taskid=? and IsContinue=0";
        List<QhTaskShare> qhTaskShares = dbServices.findListByHql(hql, intParamMap);
        if(qhTaskShares.size()>0){
            contishare(request,response);
            return;
        }

        StringBuffer url = request.getRequestURL();
        String tempContextUrl = url.delete(url.length() - request.getRequestURI().length(), url.length()).append(request.getContextPath()).append("/").toString();
        //基础参数
        QhSysPar qhSysPar=dbServices.getEntity(QhSysPar.class, 1);
        //任务信息
        QhTask qhTask = dbServices.getEntity(QhTask.class, taskid);
        //用户信息
        QhUser qhUser=dbServices.getEntity(QhUser.class, uid);

        i = 0;
        intParamMap.clear();
        intParamMap.put(i++, mobile);
        hql = "from QhTaskShare where CreateUser=? and DATE_FORMAT(CreateDateTime,'%Y-%m-%d')=DATE_FORMAT(now(),'%Y-%m-%d') and IsContinue=0";
        qhTaskShares = dbServices.findListByHql(hql, intParamMap);
        if (qhTaskShares.size() == qhSysPar.getTaskShareCnt()) {
            resultMap.put("state", "0");
            resultMap.put("data", new HashMap());
            /*if(qhUser.getIsNew()==1){
                hql="from QhUserNoviceTask where type='邀请' and createuser=? and state=99";
                List<QhUserNoviceTask> qhUserNoviceTasks = dbServices.findListByHql(hql, intParamMap);
                if(qhUserNoviceTasks.size()>0){
                    resultMap.put("msg", "请完成新手任务的邀请！");
                }else {
                    resultMap.put("msg", "新手任务中还差新手提现，请先完成新手提现再分享！");
                }
            }
            else{
                resultMap.put("msg", "每日可分享" + qhSysPar.getTaskShareCnt() + "条，分享数已用完，请明天再来");
            }*/
            resultMap.put("msg", "今日分享数（"+ qhSysPar.getTaskShareCnt() +"）已完，请在“分享中”继续分享");
            return;
        }

        if(qhUser.getIsNew()==1){
            hql="from QhUserNoviceTask where CreateUser=? and type='分享'";
            i=0;
            intParamMap.clear();
            intParamMap.put(i++, mobile);
            List<QhUserNoviceTask> qhUserNoviceTasks = dbServices.findListByHql(hql, intParamMap);
            if(qhUserNoviceTasks.size()>0){
                i=0;
                intParamMap.clear();
                intParamMap.put(i++, mobile);
                hql="from QhUserProfit where CreateUser=? and type='分享'";
                List<QhUserProfit> qhUserProfits = dbServices.findListByHql(hql, intParamMap);
                if(qhUserProfits.size()==qhUserNoviceTasks.get(0).getCnt()){
                    resultMap.put("state", "0");
                    resultMap.put("msg", "你已完成此分享任务");
                    resultMap.put("data", new HashMap());
                    return;
                }

                List<JSONObject> jsonQhTaskShare = Lists.newArrayList();
                JSONObject jobQhTaskShare = new JSONObject();
                jobQhTaskShare.put("taskId", taskid);
                jobQhTaskShare.put("cnt", 1);
                jobQhTaskShare.put("isContinue", 0);
                jobQhTaskShare.put("createUserLeave", qhUser.getLeav());
                jobQhTaskShare.put("createUser", mobile);
                jobQhTaskShare.put("createDateTime", strnowTime);
                jobQhTaskShare.put("modifyUser", mobile);
                jobQhTaskShare.put("modifyDateTime", strnowTime);
                jobQhTaskShare.put("state", 99);
                jsonQhTaskShare.add(jobQhTaskShare);
                returnId=dbServices.saveOrupdateRecords("QhTaskShare", jsonQhTaskShare, null, false);

                //添加收益记录
                QhNoviceTask qhNoviceTask=dbServices.getEntity(QhNoviceTask.class,qhUserNoviceTasks.get(0).getPid());
                addProfit(uid,taskid, "分享", qhNoviceTask.getSigleCashMoney(), mobile,"");

                //如果分享任务完成，就把状态置为1
                i=0;
                intParamMap.clear();
                intParamMap.put(i++, mobile);
                hql="from QhUserProfit where CreateUser=? and type='分享'";
                qhUserProfits = dbServices.findListByHql(hql, intParamMap);
                if(qhUserProfits.size()==qhUserNoviceTasks.get(0).getCnt()){
                    QhUserNoviceTask qhUserNoviceTask=dbServices.getEntity(QhUserNoviceTask.class,qhUserNoviceTasks.get(0).getId());
                    qhUserNoviceTask.setState(1);
                    dbServices.update(qhUserNoviceTask);

                    //发送“可以提现”通告
                    sendTaskCompleteMsg(uid, mobile);

                    //邀请好友获得1元奖励
                    ////i=0;
                    ////intParamMap.clear();
                    ////intParamMap.put(i++, qhUser.getInviter());
                    ////hql="from QhUser where mobile=?";
                    ////List<QhUser> qhUsers = dbServices.findListByHql(hql, intParamMap);
                    ////if(qhUsers.size()>0){
                        ////if(qhUser.getInviterIndex()>1){
                            //如果是属于某个人第一次邀请的徒弟，则把是否正式徒弟设为true
                            ////qhUser.setIsApprentice(1);
                            ////dbServices.update(qhUser);
                            //添加邀请人收益记录
                            ////addProfit(qhUsers.get(0).getId(), taskid, "邀请", qhSysPar.getInviteCash(), qhUsers.get(0).getMobile());

                            //升级
                            ////upgrade(qhUsers.get(0).getId());
                        ////}

                        //完成奖励任务
                        //comRewardTask_inviter(qhUsers.get(0).getId());
                        //操作日志
                        //Operlog(taskid, "邀请", "shareNoviceTask", "invite",qhUsers.get(0).getMobile(), request);
                    ////}
                }

                //操作日志
                Operlog(taskid, "分享", "shareNoviceTask", "sharenovicetask", mobile, 0,"",request);

                JSONObject jsonO = new JSONObject();
                jsonO.put("url", tempContextUrl + "app/read.jsp?iscontinue=0&taskid="+taskid+"&inviter="+mobile+"&from="+from);

                resultMap.put("state", "1");
                resultMap.put("msg", "分享成功");
                resultMap.put("data", jsonO);

            }
        }
        else {
            /*i = 0;
            intParamMap.clear();
            intParamMap.put(i++, taskid);
            hql = "select sum(CashMoney) from qh_user_profit where TaskId=?";
            Object oProfit =dbServices.findUniqueBySql(hql,intParamMap);
            Double dProfit=0.0;
            if(oProfit!=null) {
                dProfit = Double.valueOf(oProfit.toString());
            }
            if(qhTask.getShareProfit()+dProfit>qhTask.getInvestAmount()){
                //把任务状态设置为已结束
                qhTask.setState(-1);
                dbServices.update(qhTask);

                resultMap.put("state", "0");
                resultMap.put("msg", "该任务投放金额已用完");
                resultMap.put("data", new HashMap());
                return;
            }*/

            i = 0;
            intParamMap.clear();
            intParamMap.put(i++, taskid);
            intParamMap.put(i++, mobile);

            List<JSONObject> jsonQhTaskShare = Lists.newArrayList();
            JSONObject jobQhTaskShare = new JSONObject();
            jobQhTaskShare.put("taskId", taskid);
            jobQhTaskShare.put("cnt", 1);
            jobQhTaskShare.put("isContinue", 0);
            jobQhTaskShare.put("createUserLeave", qhUser.getLeav());
            jobQhTaskShare.put("createUser", mobile);
            jobQhTaskShare.put("createDateTime", strnowTime);
            jobQhTaskShare.put("modifyUser", mobile);
            jobQhTaskShare.put("modifyDateTime", strnowTime);
            jobQhTaskShare.put("state", 99);
            jsonQhTaskShare.add(jobQhTaskShare);
            returnId=dbServices.saveOrupdateRecords("QhTaskShare", jsonQhTaskShare, null, false);

            //addProfit(uid,taskid, "分享", qhTask.getShareProfit(), mobile);
            //操作日志
            Operlog(taskid, "分享", "share", "share", mobile, 0, "", request);

            JSONObject jsonO = new JSONObject();
            jsonO.put("url", tempContextUrl + "app/read.jsp?iscontinue=0&taskid="+taskid+"&inviter="+mobile+"&from="+from);

            resultMap.put("state", "1");
            resultMap.put("msg", "分享成功");
            resultMap.put("data", jsonO);

        }

        //分享6次后，设置为有效分享
        i=0;
        intParamMap.clear();
        intParamMap.put(i++, mobile);
        hql="from QhTaskShare where createuser=? and IsContinue=0";
        qhTaskShares = dbServices.findListByHql(hql, intParamMap);
        if(qhTaskShares.size()==6) {
            i = 0;
            intParamMap.clear();
            intParamMap.put(i++, qhUser.getInviter());
            hql = "from QhUser where mobile=?";
            List<QhUser> qhUsers = dbServices.findListByHql(hql, intParamMap);
            if (qhUsers.size() > 0) {
                if(qhUser.getInviterIndex()>1){
                    qhUser=dbServices.getEntity(QhUser.class,uid);
                    qhUser.setIsApprentice(1);
                    dbServices.update(qhUser);

                    //添加邀请人收益记录
                    addProfit(qhUsers.get(0).getId(), taskid, "邀请", qhSysPar.getInviteCash(), qhUsers.get(0).getMobile(), qhUser.getMobile());

                    //添加新手任务的限时收徒
                    TimeLit_Invite(qhUsers.get(0).getId());

                    //升级
                    upgrade(qhUsers.get(0).getId());

                }
            }
        }

        List<JSONObject> jsonObjects = Lists.newArrayList();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("uid", qhTask.getCreateUser());
        jsonObject.put("taskId", qhTask.getId());
        jsonObject.put("cashMoney", qhTask.getAdvPrice());
        jsonObject.put("note", qhTask.getAdvLink());
        jsonObject.put("createUser", mobile);
        jsonObject.put("createDateTime", strnowTime);
        jsonObject.put("modifyUser", mobile);
        jsonObject.put("modifyDateTime", strnowTime);
        jsonObject.put("state", 99);
        jsonObjects.add(jsonObject);
        returnId=dbServices.saveOrupdateRecords("QhCharg", jsonObjects, null, false);
        //修改广告主的总资金和余额
        User user=dbServices.getEntity(User.class,qhTask.getCreateUser());
        double TotalFund=user.getTotalFund() - qhTask.getAdvPrice();
        double AvailFund=user.getAvailFund() - qhTask.getAdvPrice();
        user.setTotalFund(TotalFund);
        user.setAvailFund(AvailFund);
        dbServices.update(user);

        //如果当天广告扣费达到预算，则停掉广告
        hql="select sum(CashMoney) from qh_charg where DATE_FORMAT(CreateDateTime,'%Y-%m-%d')=DATE_FORMAT(NOW(),'%Y-%m-%d') and taskid=?";
        i = 0;
        intParamMap.clear();
        intParamMap.put(i++, qhTask.getId());
        Object Ochargmoney=dbServices.findUniqueBySql(hql,intParamMap);
        double ChargMoney=0;
        if(Ochargmoney!=null) {
            ChargMoney = Double.valueOf(Ochargmoney.toString());
        }
        if(ChargMoney+qhTask.getAdvPrice()>qhTask.getInvestAmount()){
            qhTask.setState(0);
            dbServices.update(qhTask);
        }
        //广告主余额小于等于5元时，停掉该广告主的所有广告
        if(user.getAvailFund()<=5){
            hql="from QhTask where CreateUser=?";
            i = 0;
            intParamMap.clear();
            intParamMap.put(i++, user.getId());
            List<QhTask> qhTasks=dbServices.findListByHql(hql,intParamMap);
            for(QhTask qhTask1:qhTasks){
                qhTask1.setState(-1);
                dbServices.update(qhTask1);
            }
        }
        //奖励工具提现额度
        /*hql="select * from qh_tool_cash where type='分享' and DATE_FORMAT(NOW(),'%Y-%m-%d') between DATE_FORMAT(StartDate,'%Y-%m-%d') and DATE_FORMAT(EndDate,'%Y-%m-%d')";
        List<QhToolCash> qhToolCashs = dbServices.findListBySql(hql, new HashMap(), QhToolCash.class);
        if(qhToolCashs.size()>0){
            i=0;
            intParamMap.clear();
            intParamMap.put(i++, mobile);

            hql="from QhTaskShare where CreateUser=?";
            List shareList = dbServices.findListByHql(hql, intParamMap);
            if(shareList.size()==qhToolCashs.get(0).getNumber()) {
                i=0;
                intParamMap.clear();
                intParamMap.put(i++, qhToolCashs.get(0).getId());
                intParamMap.put(i++,mobile);
                hql="from QhToolCashUser where pid=? and createUser=?";
                List CashUserList = dbServices.findListByHql(hql, intParamMap);
                if(CashUserList.size()==0) {
                    List<JSONObject> jsonQhToolCashUser = Lists.newArrayList();
                    JSONObject jobQhToolCashUser = new JSONObject();
                    jobQhToolCashUser.put("pid", qhToolCashs.get(0).getId());
                    jobQhToolCashUser.put("createUser", mobile);
                    jobQhToolCashUser.put("createDateTime", strnowTime);
                    jobQhToolCashUser.put("modifyUser", mobile);
                    jobQhToolCashUser.put("modifyDateTime", strnowTime);
                    jobQhToolCashUser.put("state", 99);
                    jsonQhToolCashUser.add(jobQhToolCashUser);
                    dbServices.saveOrupdateRecords("QhToolCashUser", jsonQhToolCashUser, null, false);
                }
            }
        }*/

        //升级
        upgrade(uid);
        //完成奖励任务
        //comRewardTask_share(uid);
        //添加新手任务的首次分享
        addUserTaskCentre(uid, 4, "首次分享",0);
        //添加新手任务的今日分享任务
        addUserTaskCentre(uid, 7, "今日分享任务",1);
        if(qhTask!=null){
            qhTask.setModifyDateTime(nowTime);
            dbServices.update(qhTask);
        }
    }

    //继续分享
    public void contishare(HttpServletRequest request,HttpServletResponse response) throws Exception {
        JSONObject param = CodeHelper.getRequestParams(request);
        StringBuffer url = request.getRequestURL();
        String tempContextUrl = url.delete(url.length() - request.getRequestURI().length(), url.length()).append(request.getContextPath()).append("/").toString();
        Integer uid=param.getInteger("uid");
        Integer taskid=param.getInteger("taskid");
        String from=param.getString("from");

        QhUser qhUser=dbServices.getEntity(QhUser.class,uid);
        QhTask qhTask=dbServices.getEntity(QhTask.class,taskid);

        String mobile=qhUser.getMobile();

        //如果当天广告扣费达到预算
        hql="select sum(CashMoney) from qh_charg where DATE_FORMAT(CreateDateTime,'%Y-%m-%d')=DATE_FORMAT(NOW(),'%Y-%m-%d') and taskid=?";
        i = 0;
        intParamMap.clear();
        intParamMap.put(i++, qhTask.getId());
        Object Ochargmoney=dbServices.findUniqueBySql(hql,intParamMap);
        double ChargMoney=0;
        if(Ochargmoney!=null) {
            ChargMoney = Double.valueOf(Ochargmoney.toString());
        }
        if(ChargMoney+qhTask.getAdvPrice()>qhTask.getInvestAmount()){
            resultMap.put("state", "0");
            resultMap.put("msg", "扣费达到预算");
            resultMap.put("data", new HashMap());
            return;
        }
        if(qhUser.getAvailFund()<qhTask.getAdvPrice()){
            resultMap.put("state", "0");
            resultMap.put("msg", "余额不足以扣费");
            resultMap.put("data", new HashMap());
            return;
        }

        List<JSONObject> jsonQhTaskShare = Lists.newArrayList();
        JSONObject jobQhTaskShare = new JSONObject();
        jobQhTaskShare.put("taskId", taskid);
        jobQhTaskShare.put("cnt", 1);
        jobQhTaskShare.put("isContinue", 1);
        jobQhTaskShare.put("createUserLeave", qhUser.getLeav());
        jobQhTaskShare.put("createUser", mobile);
        jobQhTaskShare.put("createDateTime", strnowTime);
        jobQhTaskShare.put("modifyUser", mobile);
        jobQhTaskShare.put("modifyDateTime", strnowTime);
        jobQhTaskShare.put("state", 99);
        jsonQhTaskShare.add(jobQhTaskShare);
        returnId=dbServices.saveOrupdateRecords("QhTaskShare", jsonQhTaskShare, null, false);

        //操作日志
        Operlog(taskid, "分享", "contishare", "contishare", mobile, 0, "", request);

        JSONObject jsonO = new JSONObject();
        jsonO.put("url", tempContextUrl + "app/read.jsp?iscontinue=1&taskid="+taskid+"&inviter="+mobile+"&from="+from);

        resultMap.put("state", "1");
        resultMap.put("msg", "分享成功");
        resultMap.put("data", jsonO);

    }

    //阅读
    public String read(HttpServletRequest request,HttpServletResponse response) throws Exception {
        JSONObject param = CodeHelper.getRequestParams(request);
        Integer taskid=param.getInteger("taskid");
        Integer uid=param.getInteger("uid");
        String mobile=param.getString("mobile");
        i = 0;
        intParamMap.clear();
        intParamMap.put(i++, taskid);
        intParamMap.put(i++, uid);

        hql="from QhTaskRead where taskid=? and CreateUser=?";

        List<QhTaskRead> qhTaskReads = dbServices.findListByHql(hql, intParamMap);
        if(qhTaskReads.size()==0){
            List<JSONObject> jsonQhTaskRead = Lists.newArrayList();
            JSONObject jobQhTaskRead = new JSONObject();
            jobQhTaskRead.put("taskId", taskid);
            jobQhTaskRead.put("createUser", uid);
            jobQhTaskRead.put("createDateTime", strnowTime);
            jobQhTaskRead.put("modifyUser", uid);
            jobQhTaskRead.put("modifyDateTime", strnowTime);
            jobQhTaskRead.put("state", 99);
            jsonQhTaskRead.add(jobQhTaskRead);
            dbServices.saveOrupdateRecords("QhTaskRead", jsonQhTaskRead, null, false);

            QhTask qhTask = dbServices.getEntity(QhTask.class, taskid);
            List<JSONObject> jsonQhUserProfit = Lists.newArrayList();
            JSONObject jobQhUserProfit = new JSONObject();
            jobQhUserProfit.put("taskId", taskid);
            jobQhUserProfit.put("type", "阅读");
            jobQhUserProfit.put("cashMoney", qhTask.getReadProfit());
            jobQhUserProfit.put("createUser", uid);
            jobQhUserProfit.put("createDateTime", strnowTime);
            jobQhUserProfit.put("modifyUser", uid);
            jobQhUserProfit.put("modifyDateTime", strnowTime);
            jobQhUserProfit.put("state", 99);
            jsonQhUserProfit.add(jobQhUserProfit);
            dbServices.saveOrupdateRecords("QhUserProfit", jsonQhUserProfit, null, false);

            //操作日志
            Operlog(taskid, "阅读", "read", "read", mobile, 0,"", request);

            //奖励工具提现额度
            /*hql="select * from qh_tool_cash where type='阅读' and DATE_FORMAT(NOW(),'%Y-%m-%d') between DATE_FORMAT(StartDate,'%Y-%m-%d') and DATE_FORMAT(EndDate,'%Y-%m-%d')";
            List<QhToolCash> qhToolCashs = dbServices.findListBySql(hql, new HashMap(),QhToolCash.class);
            if(qhToolCashs.size()>0){
                i=0;
                intParamMap.clear();
                intParamMap.put(i++, uid);

                hql="from QhTaskRead where CreateUser=?";
                List QhTaskReads = dbServices.findListByHql(hql, intParamMap);
                if(QhTaskReads.size()==qhToolCashs.get(0).getNumber()) {
                    List<JSONObject> jsonQhToolCashUser = Lists.newArrayList();
                    JSONObject jobQhToolCashUser = new JSONObject();
                    jobQhToolCashUser.put("pid", qhToolCashs.get(0).getId());
                    jobQhToolCashUser.put("createUser", uid);
                    jobQhToolCashUser.put("createDateTime", strnowTime);
                    jobQhToolCashUser.put("modifyUser", uid);
                    jobQhToolCashUser.put("modifyDateTime", strnowTime);
                    jobQhToolCashUser.put("state", 99);
                    jsonQhToolCashUser.add(jobQhToolCashUser);
                    dbServices.saveOrupdateRecords("QhToolCashUser", jsonQhToolCashUser, null, false);
                }
            }*/

            resultMap.put("state", "1");
            resultMap.put("msg", "阅读成功");
            resultMap.put("data", new HashMap());
        }
        else{
            resultMap.put("state", "0");
            resultMap.put("msg", "你已阅读过");
            resultMap.put("data", new HashMap());
        }
        return "";
    }
    //获取徒弟关系
    public void getMentorShip(HttpServletRequest request,HttpServletResponse response) throws Exception {
        JSONObject param = CodeHelper.getRequestParams(request);
        Integer puid=param.getInteger("puid");
        Integer uid=param.getInteger("uid");
        String mobile=param.getString("mobile");
        hql="select a.id,a.mobile,a.uid as InviteCode,a.NickName as Name,a.PicUrl,a.ModifyDateTime as LoginDate,a.Leav,a.IsApprentice,";
        hql+="ifnull((select sum(CashMoney) from qh_user_commission where uid=? and CreateUser=a.mobile),0)NowCommission,";
        hql+="ifnull((select sum(CashMoney) from qh_user_commission where uid=? and CreateUser=a.mobile),0)TotalCommission,";
        hql+="DATEDIFF(now(),ModifyDateTime) as Days,";
        hql+="(select count(0) from qh_user where inviter=a.mobile)IsNext,";
        hql+="(SELECT count(0) from qh_user where id=a.id and DATEDIFF(now(),ModifyDateTime)>=7)IsShow";
        hql+= " from qh_user a where Inviter=? and islogin=1";

        CurrPage = param.containsKey("page") ? Integer.parseInt(param.getString("page")):1;
        RdCount = 0;
        PageCount = 0;
        i=0;
        intParamMap.clear();
        intParamMap.put(i++, puid);
        intParamMap.put(i++, puid);
        intParamMap.put(i++, mobile);
        List<QhMentorShip> qhMentorShips = dbServices.findListBySql(hql, intParamMap, QhMentorShip.class);

        RdCount=qhMentorShips.size();
        PageCount = Getpagecount(RdCount, PageSize);
        sid = Getstartpageindex(PageSize, CurrPage);
        eid = PageSize;

        intParamMap.put(i++, sid);
        intParamMap.put(i++, eid);
        hql+=" order by CreateDateTime desc limit ?,?";
        qhMentorShips = dbServices.findListBySql(hql, intParamMap, QhMentorShip.class);

        List<JSONObject> jsonObjects = Lists.newArrayList();
        for(QhMentorShip qhMentorShip:qhMentorShips){
            JSONObject jsonObject=new JSONObject();
            jsonObject.put("id",qhMentorShip.getId());
            jsonObject.put("Mobile",qhMentorShip.getMobile());
            jsonObject.put("InviteCode",qhMentorShip.getInviteCode());
            jsonObject.put("Name",qhMentorShip.getName());
            jsonObject.put("PicUrl",qhMentorShip.getPicUrl());
            jsonObject.put("LoginDate",time.format(qhMentorShip.getLoginDate()));
            jsonObject.put("NowCommission",qhMentorShip.getNowCommission());
            jsonObject.put("TotalCommission",qhMentorShip.getTotalCommission());
            jsonObject.put("Leav",qhMentorShip.getLeav());
            jsonObject.put("Days",qhMentorShip.getDays());
            jsonObject.put("IsNext",qhMentorShip.getIsNext()>0?1:0);
            jsonObject.put("IsShow",qhMentorShip.getIsShow());
            jsonObject.put("IsApprentice",qhMentorShip.getIsApprentice());
            jsonObjects.add(jsonObject);
        }
        resultMap.put("state", "1");
        resultMap.put("msg", "");
        resultMap.put("data", jsonObjects);

    }
    //获取直推人数和团队人数
    public void getMentorShip_banner(HttpServletRequest request,HttpServletResponse response) throws Exception {
        JSONObject param = CodeHelper.getRequestParams(request);
        Integer uid=param.getInteger("puid");
        QhUser qhUser=dbServices.getEntity(QhUser.class, uid);
        String mobile=qhUser.getMobile();

        i=0;
        intParamMap.clear();
        intParamMap.put(i++, mobile);
        hql="SELECT * FROM qh_user WHERE FIND_IN_SET(inviter,queryChildrenAreaInfo(?)) and islogin=1";
        List<QhUser> qhUsers = dbServices.findListBySql(hql, intParamMap);
        Integer oneCnt=qhUsers.size();

        Double totalmoney=0.0;
        hql="SELECT * FROM qh_user WHERE FIND_IN_SET(inviter,queryChildrenAreaInfo(?)) and islogin=1";
        qhUsers = dbServices.findListBySql(hql, intParamMap, QhUser.class);
        for(QhUser qhUser1:qhUsers){
            i=0;
            intParamMap.clear();
            intParamMap.put(i++, uid);
            intParamMap.put(i++, qhUser1.getMobile());
            hql="select sum(CashMoney) from qh_user_commission where uid=? and CreateUser=? ";
            Object Ocashmoney=dbServices.findUniqueBySql(hql,intParamMap);
            if(Ocashmoney!=null) {
                totalmoney += Double.valueOf(Ocashmoney.toString());
            }
        }

        BigDecimal bg = new BigDecimal(totalmoney);

        JSONObject jsonObject=new JSONObject();
        jsonObject.put("oneCnt",bg.setScale(2, BigDecimal.ROUND_HALF_UP));
        jsonObject.put("totalCnt",oneCnt);

        resultMap.put("state", "1");
        resultMap.put("msg", "");
        resultMap.put("data", jsonObject);
    }

    //获取当前等级的升级剩余条件
    public void getResCond(HttpServletRequest request,HttpServletResponse response) throws Exception {
        JSONObject param = CodeHelper.getRequestParams(request);
        Integer leav=param.getInteger("leav");
        Integer leave=leav+1;
        Integer uid=param.getInteger("uid");
        String mobile=param.getString("mobile");

        JSONObject jsonObj = new JSONObject();

        if(leave==10){
            jsonObj.put("InviteCnt",0);
            jsonObj.put("TaskCnt",0);

            resultMap.put("state", "1");
            resultMap.put("msg", "");
            resultMap.put("data", jsonObj);
            return;
        }

        QhSysPar qhSysPar=dbServices.getEntity(QhSysPar.class, 1);
        QhUser qhUser=dbServices.getEntity(QhUser.class, uid);

        QhSysParLeave qhSysParLeave=dbServices.getEntity(QhSysParLeave.class, leav + 1);
        Integer InviteCnt=qhSysParLeave.getInviteCnt();
        Integer TaskCnt=qhSysParLeave.getTaskCnt();

        i=0;
        intParamMap.clear();
        intParamMap.put(i++, mobile);
        hql="select count(0) from qh_user where inviter=? and IsApprentice=1";
        if(leave>5){
            intParamMap.put(i++, leave-1);
            hql+=" and Leav>=?";
        }
        Integer InviteCntByUid=0;
        Object OinviteCntByUid=dbServices.findUniqueBySql(hql, intParamMap);
        InviteCntByUid=Integer.parseInt(OinviteCntByUid.toString());

        i=0;
        intParamMap.clear();
        intParamMap.put(i++, mobile);
        hql="select count(0) from qh_task_share where CreateUser=? and IsContinue=0";
        Integer TaskCntByUid=0;
        Object OtaskCntByUid=dbServices.findUniqueBySql(hql, intParamMap);
        TaskCntByUid=Integer.parseInt(OtaskCntByUid.toString());

        jsonObj.put("InviteCnt",InviteCnt<InviteCntByUid?0:InviteCnt-InviteCntByUid);
        jsonObj.put("TaskCnt",TaskCnt<TaskCntByUid?0:TaskCnt-TaskCntByUid);

        resultMap.put("state", "1");
        resultMap.put("msg", "");
        resultMap.put("data", jsonObj);

    }

    //获取会员等级
    public void getSysParLeaveList(HttpServletRequest request,HttpServletResponse response) throws Exception {
        hql="from QhSysParLeave";
        List<QhSysParLeave> qhSysParLeaves = dbServices.findListByHql(hql,new HashMap());
        resultMap.put("state", "1");
        resultMap.put("msg", "");
        resultMap.put("data", qhSysParLeaves);
    }
    //绑定支付宝
    public void editAlipay(HttpServletRequest request, HttpServletResponse response) throws Exception {
        JSONObject param = CodeHelper.getRequestParams(request);
        Integer uid=param.getInteger("uid");
        String alipayName=param.getString("alipayName");
        String alipay=param.getString("alipay");
        QhUser qhUser = dbServices.getEntity(QhUser.class,uid);
        qhUser.setAlipay(alipay);
        qhUser.setAlipayName(alipayName);
        dbServices.update(qhUser);

        resultMap.put("state", "1");
        resultMap.put("msg", "绑定支付宝成功");
        resultMap.put("data", qhUser);

        //操作日志
        Operlog(0, "绑定支付宝", "editAlipay", "", qhUser.getMobile(), 0, "", request);
    }

    //修改密码
    public void modifypwd(HttpServletRequest request,HttpServletResponse response) throws Exception {
        JSONObject param = CodeHelper.getRequestParams(request);
        Integer uid=param.getInteger("uid");
        String oldpwd=EncryptUtils.encrypt(param.getString("oldpwd"));
        String newpwd=EncryptUtils.encrypt(param.getString("newpwd"));

        QhUser qhUser=dbServices.getEntity(QhUser.class, uid);
        if (!qhUser.getPwd().equals(oldpwd)){
            resultMap.put("state", "0");
            resultMap.put("msg", "原密码错误");
            resultMap.put("data", new HashMap());
            return;
        }
        qhUser.setPwd(newpwd);
        dbServices.update(qhUser);
        resultMap.put("state", "1");
        resultMap.put("msg", "密码修改成功");
        resultMap.put("data", qhUser);

        //操作日志
        Operlog(0, "修改密码", "modifypwd", "", qhUser.getMobile(), 0, "", request);
    }
    //修改头像
    public void modifyHeadImg(HttpServletRequest request) throws Exception {
        System.out.println("修改头像");
        //String headimg=uploads(file);

        JSONObject param = CodeHelper.getRequestParams(request);
        Integer uid=param.getInteger("uid");
        QhUser qhUser=dbServices.getEntity(QhUser.class, uid);
        qhUser.setPicUrl(param.getString("headimg"));
        dbServices.update(qhUser);
        resultMap.put("state", "1");
        resultMap.put("msg", "修改头像成功");
        resultMap.put("data", qhUser);

        //操作日志
        Operlog(0,"修改头像", "modifyHeadImg","", qhUser.getMobile(),0,"",request);
    }
    //修改用户信息
    public void modifyUserInfo(HttpServletRequest request,HttpServletResponse response) throws Exception {
        JSONObject param = CodeHelper.getRequestParams(request);
        Integer uid=param.getInteger("uid");
        String nickname=param.getString("nickname");
        String mobile=param.getString("mobile");
        String sex=param.getString("sex");
        Date birthday=param.getDate("birthday");
        QhUser qhUser= dbServices.getEntity(QhUser.class, uid);
        if(nickname!=null&&!nickname.equals("")){
            hql="from QhUser where Inviter=?";
            i=0;
            intParamMap.clear();
            intParamMap.put(i++, qhUser.getMobile());
            List<QhUser> qhUsers = dbServices.findListByHql(hql,intParamMap);
            for(QhUser qhUser1 :qhUsers){
                qhUser1.setInviterNickName(nickname);
                dbServices.update(qhUser1);
            }

            qhUser.setNickName(nickname);
        }
        if(mobile!=null&&!mobile.equals("")){
            qhUser.setMobile(mobile);
        }
        if(sex!=null&&!sex.equals("")){
            qhUser.setSex(sex);
        }
        if(birthday!=null&&!birthday.equals("")){
            qhUser.setBirthday(birthday);
        }
        dbServices.update(qhUser);
        resultMap.put("state", "1");
        resultMap.put("msg", "修改用户信息成功");
        resultMap.put("data", qhUser);

        //操作日志
        Operlog(0,"修改用户信息", "modifyUserInfo", "",qhUser.getMobile(),0,"",request);
    }
    //实名认证
    public void realNameCert(HttpServletRequest request,HttpServletResponse response) throws Exception {
        JSONObject param = CodeHelper.getRequestParams(request);
        Integer uid=param.getInteger("uid");
        String name=param.getString("name");
        String idCard=param.getString("idCard");

        hql="from QhUser where IdCard=?";
        i=0;
        intParamMap.clear();
        intParamMap.put(i++, idCard);
        List<QhUser> qhUsers = dbServices.findListByHql(hql,intParamMap);
        if(qhUsers.size()>0){
            resultMap.put("state", "0");
            resultMap.put("msg", "该身份证已经认证过");
            resultMap.put("data", qhUsers.get(0));
        }
        else {
            QhUser qhUser = dbServices.getEntity(QhUser.class, uid);
            qhUser.setName(name);
            qhUser.setIdCard(idCard);
            qhUser.setIsRealName(1);
            dbServices.update(qhUser);
            resultMap.put("state", "1");
            resultMap.put("msg", "实名认证成功");
            resultMap.put("data", qhUser);

            //操作日志
            Operlog(0, "实名认证", "realNameCert", "", qhUser.getMobile(), 0,"", request);
        }
    }
    //修改微信
    public void modifyWechat(HttpServletRequest request,HttpServletResponse response) throws Exception {
        JSONObject param = CodeHelper.getRequestParams(request);
        Integer uid=param.getInteger("uid");
        String wechat=param.getString("wechat");

        hql="from QhUser where wechat=?";
        i=0;
        intParamMap.clear();
        intParamMap.put(i++, wechat);
        List<QhUser> qhUsers = dbServices.findListByHql(hql, intParamMap);
        if(qhUsers.size()>0){
            resultMap.put("state", "0");
            resultMap.put("msg", "该微信验证码已经绑定过");
            resultMap.put("data", new HashMap());
            return;
        }

        QhUser qhUser=dbServices.getEntity(QhUser.class, uid);
        qhUser.setWechat(wechat);
        dbServices.update(qhUser);
        resultMap.put("state", "1");
        resultMap.put("msg", "修改微信成功");
        resultMap.put("data", qhUser);

        //操作日志
        Operlog(0, "修改微信", "modifyWechat", "", qhUser.getMobile(), 0, "", request);
    }
    //获取QQ客服
    public void getQQService(HttpServletRequest request, HttpServletResponse response) throws Exception {
        QhSysPar qhSysPar=dbServices.getEntity(QhSysPar.class, 1);
        JSONObject jsonObj = new JSONObject();
        jsonObj.put("QQ1", qhSysPar.getQqSer1());
        jsonObj.put("QQ2", qhSysPar.getQqSer2());
        jsonObj.put("QQ3", qhSysPar.getQqSer3());
        jsonObj.put("QQ4", qhSysPar.getQqSer4());

        resultMap.put("state", "1");
        resultMap.put("msg", "");
        resultMap.put("data", jsonObj);
    }
    //获取版本号
    public void getVersion(HttpServletRequest request,HttpServletResponse response) throws Exception {
        QhSysPar qhSysPar=dbServices.getEntity(QhSysPar.class, 1);
        JSONObject jsonObj = new JSONObject();
        jsonObj.put("Version", qhSysPar.getVersion());
        jsonObj.put("Apk", qhSysPar.getApk());

        resultMap.put("state", "1");
        resultMap.put("msg", "");
        resultMap.put("data", jsonObj);
    }
    //获取QQ群
    public void getQQG(HttpServletRequest request,HttpServletResponse response) throws Exception {
        QhSysPar qhSysPar=dbServices.getEntity(QhSysPar.class, 1);
        JSONObject jsonObj = new JSONObject();
        jsonObj.put("QQG", qhSysPar.getQqG());
        jsonObj.put("QQKey", qhSysPar.getQqKey());

        resultMap.put("state", "1");
        resultMap.put("msg", "");
        resultMap.put("data", jsonObj);
    }
    //留言反馈
    public void addQhFeedback(HttpServletRequest request,HttpServletResponse response) throws Exception {
        JSONObject param = CodeHelper.getRequestParams(request);
        Integer uid=param.getInteger("uid");
        String mobile=param.getString("mobile");
        String type=param.getString("type");
        String note=param.getString("note");

        QhUser qhUser=dbServices.getEntity(QhUser.class,uid);

        List<JSONObject> jsonQhFeedback= Lists.newArrayList();
        JSONObject jobQhFeedback = new JSONObject();
        jobQhFeedback.put("type", type);
        jobQhFeedback.put("note", note);
        jobQhFeedback.put("createUser", qhUser.getMobile());
        jobQhFeedback.put("createDateTime", strnowTime);
        jobQhFeedback.put("modifyUser",  qhUser.getMobile());
        jobQhFeedback.put("modifyDateTime", strnowTime);
        jobQhFeedback.put("state", 99);
        jsonQhFeedback.add(jobQhFeedback);
        dbServices.saveOrupdateRecords("QhFeedback", jsonQhFeedback, null, false);

        resultMap.put("state", "1");
        resultMap.put("msg", "反馈成功");
        resultMap.put("data", new HashMap());

        //操作日志
        Operlog(0, "留言反馈", "addQhFeedback", "", mobile, 0, "", request);
    }
    //获取系统公告
    public void getNoticeList(HttpServletRequest request, HttpServletResponse response) throws Exception {
        JSONObject param = CodeHelper.getRequestParams(request);
        String uid=param.getString("uid");
        i=0;
        intParamMap.clear();
        intParamMap.put(i++, uid);
        hql="from QhNotice where uid=? order by createdatetime desc";
        List<QhNotice> qhNotices = dbServices.findListByHql(hql, intParamMap);
        List<JSONObject> jsonObjects= Lists.newArrayList();

        StringBuffer url = request.getRequestURL();
        String tempContextUrl = url.delete(url.length() - request.getRequestURI().length(), url.length()).append(request.getContextPath()).append("/").toString();

        for(QhNotice qhNotice:qhNotices){
            JSONObject jsonObject = new JSONObject();
            if(qhNotice.getType().equals("收益")){
                jsonObject.put("icon", 1);
            }
            else{
                jsonObject.put("icon", 2);
            }
            jsonObject.put("url", tempContextUrl+"app/notice_view.jsp?id="+qhNotice.getId());
            jsonObject.put("type", qhNotice.getType());
            jsonObject.put("title", qhNotice.getTitle());
            jsonObject.put("state", qhNotice.getState());
            jsonObject.put("createUser", qhNotice.getCreateUser());
            jsonObject.put("createDateTime", time.format(qhNotice.getCreateDateTime()));
            jsonObjects.add(jsonObject);
        }
        resultMap.put("state", "1");
        resultMap.put("msg", "");
        resultMap.put("data", jsonObjects);
    }
    //获取系统公告详情
    public void getNoticeView(HttpServletRequest request,HttpServletResponse response) throws Exception {
        JSONObject param = CodeHelper.getRequestParams(request);
        Integer id=param.getInteger("id");
        QhNotice qhNotice=dbServices.getEntity(QhNotice.class, id);
        resultMap.put("state", "1");
        resultMap.put("msg", "");
        resultMap.put("data", qhNotice);
    }
    //设置系统公告为已读
    public void setNoticeState(HttpServletRequest request,HttpServletResponse response) throws Exception {
        JSONObject param = CodeHelper.getRequestParams(request);
        String uid=param.getString("uid");
        i=0;
        intParamMap.clear();
        intParamMap.put(i++, uid);
        hql="update qh_notice set state=1 where uid=? ";
        dbServices.executeBySql(hql, intParamMap);
        resultMap.put("state", "1");
        resultMap.put("msg", "设置成功");
        resultMap.put("data", new HashMap());
    }
    //邀请赚钱
    public void getInviteInfo(HttpServletRequest request,HttpServletResponse response) throws Exception {
        StringBuffer url = request.getRequestURL();
        String tempContextUrl = url.delete(url.length() - request.getRequestURI().length(), url.length()).append(request.getContextPath()).append("/").toString();

        JSONObject param = CodeHelper.getRequestParams(request);
        Integer uid=param.getInteger("uid");
        String mobile=param.getString("mobile");
        i=0;
        intParamMap.clear();
        intParamMap.put(i++, mobile);
        hql="select sum(CashMoney) from qh_user_profit where CreateUser=? and type='邀请'";
        Double CashMoney=0.0;
        Object OcashMoney=dbServices.findUniqueBySql(hql, intParamMap);
        if(OcashMoney!=null) {
           CashMoney = Double.valueOf(OcashMoney.toString());
        }

        hql="select count(0) from qh_user where Inviter=? and IsApprentice=1";
        Object Ocnt=dbServices.findUniqueBySql(hql, intParamMap);
        Integer Cnt=0;
        if(Ocnt!=null) {
            Cnt = Integer.parseInt(Ocnt.toString());
        }

        QhUser qhUser=dbServices.getEntity(QhUser.class, uid);
        JSONObject jsonObj = new JSONObject();
        jsonObj.put("CashMoney", CashMoney);
        jsonObj.put("Cnt", Cnt);
        jsonObj.put("Leave", qhUser.getLeav());
        jsonObj.put("LeaveIcon", tempContextUrl+"static/images/member"+qhUser.getLeav()+".png");

        resultMap.put("state", "1");
        resultMap.put("msg", "");
        resultMap.put("data", jsonObj);
    }
    //获取提现账户
    public void getCashAccount(HttpServletRequest request,HttpServletResponse response) throws Exception {
        JSONObject param = CodeHelper.getRequestParams(request);
        Integer uid=param.getInteger("uid");
        QhUser qhUser=dbServices.getEntity(QhUser.class, uid);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("Alipay", qhUser.getAlipay());
        jsonObject.put("Wechat", qhUser.getWechat());
        resultMap.put("state", "1");
        resultMap.put("msg", "");
        resultMap.put("data", jsonObject);
    }
    public void getCode(HttpServletRequest request,HttpServletResponse response) throws Exception {
        List<JSONObject> jsonQhCode= Lists.newArrayList();
        JSONObject param = CodeHelper.getRequestParams(request);
        String mobile=param.getString("mobile");
        String type=param.getString("type");
        if(type==null){
            type="";
        }

        if(type.equals("register")) {
            i = 0;
            intParamMap.clear();
            intParamMap.put(i++, mobile);
            String hql = "from QhUser where mobile=?";
            List qhUsers = dbServices.findListByHql(hql, intParamMap);
            if (qhUsers.size() > 0) {
                resultMap.put("state", "0");
                resultMap.put("msg", "该手机号已注册");
                resultMap.put("data", new HashMap());
                return;
            }
        }

        String code = "";
        for (int i = 0; i < 4; i++) {
            code +=(int)(Math.random()*10);
        }

        tool.testTemplateSMS(true,"984f18df99ff4a89cc7be5c9e0ac4a98","51940239159462d001395b14359ab95b","0d1ebf30ec2e49e8afd39cb2b98d34f0","102715",mobile, code);

        JSONObject jobQhCode = new JSONObject();
        jobQhCode.put("code", code);
        jobQhCode.put("mobile", mobile);
        jobQhCode.put("createUser", mobile);
        jobQhCode.put("createDateTime", strnowTime);
        jobQhCode.put("modifyUser", mobile);
        jobQhCode.put("modifyDateTime", strnowTime);
        jobQhCode.put("state", 99);
        jsonQhCode.add(jobQhCode);
        dbServices.saveOrupdateRecords("QhCode", jsonQhCode, null, false);

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code",code);

        resultMap.put("state", "1");
        resultMap.put("msg", "获取验证码成功");
        resultMap.put("data", jsonObject);

    }

    //获取提现规则
    public void getCashRule(HttpServletRequest request,HttpServletResponse response) throws Exception {
        QhSysPar qhSysPar=dbServices.getEntity(QhSysPar.class, 1);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("cashrule", qhSysPar.getCashRule());

        resultMap.put("state", "1");
        resultMap.put("msg", "");
        resultMap.put("data", jsonObject);
    }
    //获取攻略入口
    public void getRaiderEntrance(HttpServletRequest request,HttpServletResponse response) throws Exception {
        StringBuffer url = request.getRequestURL();
        String tempContextUrl = url.delete(url.length() - request.getRequestURI().length(), url.length()).append(request.getContextPath()).append("/").toString();

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("url",tempContextUrl+"app/RaiderEntrance.jsp");
        resultMap.put("state", "1");
        resultMap.put("msg", "");
        resultMap.put("data", jsonObject);
    }
    //获取赚钱攻略
    public void getMoneyRaider(HttpServletRequest request,HttpServletResponse response) throws Exception {
        StringBuffer url = request.getRequestURL();
        String tempContextUrl = url.delete(url.length() - request.getRequestURI().length(), url.length()).append(request.getContextPath()).append("/").toString();

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("url",tempContextUrl+"app/MoneyRaider.jsp");
        resultMap.put("state", "1");
        resultMap.put("msg", "");
        resultMap.put("data", jsonObject);
    }
    //获取赚钱规则
    public void getMoneyRule(HttpServletRequest request,HttpServletResponse response) throws Exception {
        StringBuffer url = request.getRequestURL();
        String tempContextUrl = url.delete(url.length() - request.getRequestURI().length(), url.length()).append(request.getContextPath()).append("/").toString();

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("url",tempContextUrl+"app/MoneyRule.jsp");
        resultMap.put("state", "1");
        resultMap.put("msg", "");
        resultMap.put("data", jsonObject);
    }
    //获取常见问题
    public void getComProblem(HttpServletRequest request,HttpServletResponse response) throws Exception {
        StringBuffer url = request.getRequestURL();
        String tempContextUrl = url.delete(url.length() - request.getRequestURI().length(), url.length()).append(request.getContextPath()).append("/").toString();

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("url",tempContextUrl+"app/ComProblem.jsp");
        resultMap.put("state", "1");
        resultMap.put("msg", "");
        resultMap.put("data", jsonObject);
    }

    public void getDemo(HttpServletRequest request,HttpServletResponse response) throws Exception {
        StringBuffer url = request.getRequestURL();
        String tempContextUrl = url.delete(url.length() - request.getRequestURI().length(), url.length()).append(request.getContextPath()).append("/").toString();

        JSONObject param = CodeHelper.getRequestParams(request);

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("url",tempContextUrl+"app/demo_list.jsp?uid="+param.getInteger("uid"));
        resultMap.put("state", "1");
        resultMap.put("msg", "");
        resultMap.put("data", jsonObject);
    }
    //获取关于钱狐
    public void getAboutUs(HttpServletRequest request,HttpServletResponse response) throws Exception {
        StringBuffer url = request.getRequestURL();
        String tempContextUrl = url.delete(url.length() - request.getRequestURI().length(), url.length()).append(request.getContextPath()).append("/").toString();

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("url",tempContextUrl+"app/aboutus.jsp");
        resultMap.put("state", "1");
        resultMap.put("msg", "");
        resultMap.put("data", jsonObject);
    }
    //获取邀请赚钱地址
    public void getInviteReg(HttpServletRequest request,HttpServletResponse response) throws Exception {
        StringBuffer url = request.getRequestURL();
        String tempContextUrl = url.delete(url.length() - request.getRequestURI().length(), url.length()).append(request.getContextPath()).append("/").toString();

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("url",tempContextUrl+"app/invite_reg.jsp");
        resultMap.put("state", "1");
        resultMap.put("msg", "");
        resultMap.put("data", jsonObject);
    }
    //获取关于钱狐
    public void getRegProtocol(HttpServletRequest request,HttpServletResponse response) throws Exception {
        StringBuffer url = request.getRequestURL();
        String tempContextUrl = url.delete(url.length() - request.getRequestURI().length(), url.length()).append(request.getContextPath()).append("/").toString();

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("url", tempContextUrl + "app/RegProtocol.jsp");
        resultMap.put("state", "1");
        resultMap.put("msg", "");
        resultMap.put("data", jsonObject);
    }

    public void getAccessToken(HttpServletRequest request,HttpServletResponse response) throws Exception {
        JSONObject param = CodeHelper.getRequestParams(request);
        Integer uid=param.getInteger("uid");
        QhUser qhUser=dbServices.getEntity(QhUser.class, uid);
        qhUser.setModifyDateTime(new Date());
        dbServices.update(qhUser);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("AccessToken", qhUser.getAccessToken());
        resultMap.put("state", "1");
        resultMap.put("msg", "");
        resultMap.put("data", jsonObject);

    }
    //激活
    public void activation(HttpServletRequest request,HttpServletResponse response) throws Exception {
        JSONObject param = CodeHelper.getRequestParams(request);
        i = 0;
        intParamMap.clear();
        intParamMap.put(i++, param.getString("mobileID"));
        hql = "from QhUserOperLog where mobileId=?";
        List<QhUserOperLog> qhUserOperLogs = dbServices.findListByHql(hql, intParamMap);
        if(qhUserOperLogs.size()==0) {
            Operlog(0, "激活", "activation", "", "", 0, "", request);
            resultMap.put("state", "1");
            resultMap.put("msg", "激活成功");
            resultMap.put("data", new HashMap());
        }
        else{
            resultMap.put("state", "0");
            resultMap.put("msg", "已激活");
            resultMap.put("data", new HashMap());
        }
    }
    //修改推荐人
    public void updateInviter(HttpServletRequest request,HttpServletResponse response) throws Exception {
        JSONObject param = CodeHelper.getRequestParams(request);
        Integer uid=param.getInteger("uid");
        String mobile=param.getString("mobile");

        QhSysPar qhSysPar = dbServices.getEntity(QhSysPar.class, 1);

        i = 0;
        intParamMap.clear();
        intParamMap.put(i++, mobile);
        hql = "from QhUser where uid=?";
        List<QhUser> qhUsers = dbServices.findListByHql(hql, intParamMap);
        if(qhUsers.size()>0) {
            QhUser qhUser = dbServices.getEntity(QhUser.class, uid);
            if(qhUser.getMobile().equals(qhUsers.get(0).getMobile())){
                resultMap.put("state", "0");
                resultMap.put("msg", "推荐人不可以填自己");
                resultMap.put("data", new HashMap());
                return;
            }

            i = 0;
            intParamMap.clear();
            intParamMap.put(i++, uid);
            hql="SELECT * FROM qh_user WHERE LOCATE(?, InMobile)>0";
            List<QhUser> qhUsers1 = dbServices.findListBySql(hql, intParamMap, QhUser.class);
            if(qhUsers1.size()>0){
                resultMap.put("state", "0");
                resultMap.put("msg", "推荐人不可以是自己徒弟徒孙");
                resultMap.put("data", new HashMap());
                return;
            }

            String InUid=qhUsers.get(0).getUid();
            String InMobile=qhUsers.get(0).getMobile();
            String InName=qhUsers.get(0).getName();
            String InNickName=qhUsers.get(0).getNickName();
            qhUser.setInviter(InMobile);

            i=0;
            intParamMap.clear();
            intParamMap.put(i++, InMobile);
            hql = "from QhUser where inviter=? and islogin=1";
            List<QhUser> inviterCnt = dbServices.findListByHql(hql, intParamMap);
            qhUser.setInviterIndex(inviterCnt.size() + 1);

            qhUser.setInviterName(InName == null || InName.equals("") ? InUid : InName);
            qhUser.setInviterNickName(InNickName == null || InNickName.equals("") ? InUid : InNickName);
            dbServices.update(qhUser);

            //设置祖先
            getInMobile(uid, InMobile);

            resultMap.put("state", "1");
            resultMap.put("msg", "");
            resultMap.put("data", qhUser);

            i = 0;
            intParamMap.clear();
            intParamMap.put(i++, InMobile);
            hql = "from QhUserNoviceTask where CreateUser=? and type='邀请' and state=99";
            List<QhUserNoviceTask> qhUserNoviceTasks = dbServices.findListByHql(hql, intParamMap);
            if(qhUserNoviceTasks.size()>0){
                QhUserNoviceTask qhUserNoviceTask=dbServices.getEntity(QhUserNoviceTask.class,qhUserNoviceTasks.get(0).getId());
                qhUserNoviceTask.setState(1);
                dbServices.update(qhUserNoviceTask);
            }

            //邀请好友获得收益
            /*hql="from QhUserOperLog where optname='inviter' and CreateUser=?";
            List<QhUserOperLog> qhUserOperLogs = dbServices.findListByHql(hql, intParamMap);
            if(qhUserOperLogs.size()>0) {
               addProfit(qhUsers.get(0).getId(), 0, "邀请", qhSysPar.getInviteCash(), InMobile);
            }*/

            Operlog(0, "邀请", "register", "inviter", InMobile, qhUser.getLeav(), "", request);
            //如果是属于某个人第一次邀请的徒弟，则把是否正式徒弟设为true
            if(qhUser.getInviterIndex()==1) {
                qhUser.setIsApprentice(1);
                dbServices.update(qhUser);

                //添加邀请人收益记录
                addProfit(qhUsers.get(0).getId(), 0, "邀请", qhSysPar.getInviteCash(), InMobile, qhUser.getMobile());
                //添加新手任务的限时收徒
                TimeLit_Invite(qhUsers.get(0).getId());
                //添加新手任务的首次邀请
                addUserTaskCentre(qhUsers.get(0).getId(), 3, "首次邀请", 0);
            }
            else{
                i = 0;
                intParamMap.clear();
                intParamMap.put(i++, qhUser.getMobile());
                hql="from QhTaskShare where createuser=? and IsContinue=0";
                List<QhTaskShare> qhTaskShares = dbServices.findListByHql(hql, intParamMap);
                if(qhTaskShares.size()>5){
                    qhUser.setIsApprentice(1);
                    dbServices.update(qhUser);

                    //添加邀请人收益记录
                    addProfit(qhUsers.get(0).getId(), 0, "邀请", qhSysPar.getInviteCash(), InMobile,qhUser.getMobile());
                    //添加新手任务的限时收徒
                    TimeLit_Invite(qhUsers.get(0).getId());
                    //添加新手任务的首次邀请
                    addUserTaskCentre(qhUsers.get(0).getId(), 3, "首次邀请", 0);
                }
            }
            //升级
            upgrade(qhUsers.get(0).getId());
            //完成奖励任务
            //comRewardTask_inviter(qhUsers.get(0).getId());

        }
        else{
            resultMap.put("state", "0");
            resultMap.put("msg", "邀请码不存在");
            resultMap.put("data", new HashMap());
        }
    }

    //获取二维码
    public void getQRcode(HttpServletRequest request,HttpServletResponse response) throws Exception {
        JSONObject param = CodeHelper.getRequestParams(request);
        StringBuffer url = request.getRequestURL();
        String tempContextUrl = url.delete(url.length() - request.getRequestURI().length(), url.length()).append(request.getContextPath()).append("/").toString();

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("url", tempContextUrl + "app/QRcode.jsp?inviter=" + param.getString("mobile"));
        resultMap.put("state", "1");
        resultMap.put("msg", "");
        resultMap.put("data", jsonObject);
    }

    //新增地址
    public void addAddress(HttpServletRequest request,HttpServletResponse response) throws Exception {
        JSONObject param = CodeHelper.getRequestParams(request);
        Integer uid=param.getInteger("uid");
        QhUser qhUser = dbServices.getEntity(QhUser.class, uid);
        String mobile=qhUser.getMobile();
        Operlog(0, "", "addAddress", "", mobile, 0, param.getString("address"), request);
        resultMap.put("state", "1");
        resultMap.put("msg", "新增数据成功");
        resultMap.put("data", new HashMap());
    }
    //获取推荐人信息
    public void getRecommendInfo(HttpServletRequest request,HttpServletResponse response) throws Exception {
        JSONObject param = CodeHelper.getRequestParams(request);
        String inviter=param.getString("inviter");
        i=0;
        intParamMap.clear();
        intParamMap.put(i++, inviter);
        hql="from QhUser where uid=?";
        List<QhUser> qhUsers = dbServices.findListByHql(hql, intParamMap);
        if(qhUsers.size()>0) {
           resultMap.put("state", "1");
           resultMap.put("msg", "");
           resultMap.put("data", qhUsers.get(0));
        }
        else{
            resultMap.put("state", "1");
            resultMap.put("msg", "邀请码不存在");
            resultMap.put("data", new HashMap());
        }
    }
    //判断是否能分享
    public void isShare(HttpServletRequest request,HttpServletResponse response) throws Exception {
        JSONObject param = CodeHelper.getRequestParams(request);
        Integer uid=param.getInteger("uid");
        QhUser qhUser=dbServices.getEntity(QhUser.class, uid);
        QhSysPar qhSysPar=dbServices.getEntity(QhSysPar.class, 1);
        i = 0;
        intParamMap.clear();
        intParamMap.put(i++, qhUser.getMobile());

        List<QhTaskShare> qhTaskShares = null;
        /*if (qhTaskShares.size() == qhSysPar.getTaskShareCnt()) {
            resultMap.put("state", "0");
            resultMap.put("data", new HashMap());
            if(qhUser.getIsNew()==1){
                hql="from QhUserNoviceTask where type='邀请' and createuser=? and state=99";
                List<QhUserNoviceTask> qhUserNoviceTasks = dbServices.findListByHql(hql, intParamMap);
                if(qhUserNoviceTasks.size()>0){
                    resultMap.put("msg", "请完成新手任务的邀请！");
                }else {
                    resultMap.put("state", "2");
                    resultMap.put("msg", "新手任务中还差新手提现，请先完成新手提现再分享！");
                }
            }
            else{
                resultMap.put("msg", "每日可分享" + qhSysPar.getTaskShareCnt() + "条，分享数已用完，请明天再来");
            }
            resultMap.put("msg", "今日分享数（"+ qhSysPar.getTaskShareCnt() +"）已完，请在“分享中”继续分享");
            return;
        }*/
        if(qhUser.getIsNew()==1){
            hql = "from QhTaskShare where CreateUser=? and IsContinue=0";
            qhTaskShares = dbServices.findListByHql(hql, intParamMap);
            hql="from QhNoviceTask where type='分享'";
            List<QhNoviceTask> qhNoviceTasks = dbServices.findListByHql(hql, new HashMap());
            if(qhNoviceTasks.size()>0){
                if (qhTaskShares.size()==qhNoviceTasks.get(0).getCnt()){
                    i = 0;
                    intParamMap.clear();
                    intParamMap.put(i++, qhUser.getMobile());
                    hql="from QhUserNoviceTask where type='邀请' and createuser=? and state=99";
                    List<QhUserNoviceTask> qhUserNoviceTasks = dbServices.findListByHql(hql, intParamMap);
                    if(qhUserNoviceTasks.size()>0){
                        resultMap.put("state", "0");
                        resultMap.put("msg", "请完成新手任务的邀请！");
                        resultMap.put("data", new HashMap());
                    }else {
                        resultMap.put("state", "2");
                        resultMap.put("msg", "新手任务中还差新手提现，请先完成新手提现再分享！");
                        resultMap.put("data", new HashMap());
                    }
                    return;
                }
            }

        }
        else {
            hql = "from QhTaskShare where CreateUser=? and DATE_FORMAT(CreateDateTime,'%Y-%m-%d')=DATE_FORMAT(now(),'%Y-%m-%d') and IsContinue=0";
            qhTaskShares = dbServices.findListByHql(hql, intParamMap);
            if (qhTaskShares.size() == qhSysPar.getTaskShareCnt()) {
                resultMap.put("state", "0");
                resultMap.put("data", new HashMap());
                resultMap.put("msg", "今日分享数（"+ qhSysPar.getTaskShareCnt() +"）已完，请在“分享中”继续分享");
                return;
            }
        }
        resultMap.put("state", "1");
        resultMap.put("msg", "");
        resultMap.put("data", new HashMap());
    }
    //唤醒
    public void wakeUp(HttpServletRequest request,HttpServletResponse response) throws Exception {
        JSONObject param = CodeHelper.getRequestParams(request);
        String operMobile="";
        Integer uid=param.getInteger("uid");
        Integer recipients=param.getInteger("recipients");

        if(recipients!=null) {
            QhUser qhUser=dbServices.getEntity(QhUser.class, recipients);
            addNotice(qhUser.getId(),qhUser.getMobile(),"a","通知","伙伴唤醒","亲爱的伙伴，你已经7天没有在钱狐上赚钱了，请马上上线吧",uid);

        }
        else {
            hql = "from QhUser where DATEDIFF(now(),ModifyDateTime)>=7";
            List<QhUser> qhUsers = dbServices.findListByHql(hql, new HashMap());
            for(QhUser qhUser:qhUsers){
                addNotice(qhUser.getId(),qhUser.getMobile(),"a","通知","伙伴唤醒","亲爱的伙伴，你已经7天没有在钱狐上赚钱了，请马上上线吧",uid);

            }
        }
        resultMap.put("state", "1");
        resultMap.put("msg", "唤醒成功");
        resultMap.put("data", new HashMap());
    }

    //是否显示唤醒按钮
    public void isShowWakeUp(HttpServletRequest request,HttpServletResponse response) throws Exception {
        JSONObject param = CodeHelper.getRequestParams(request);
        Integer uid=param.getInteger("uid");
        i = 0;
        intParamMap.clear();
        intParamMap.put(i++, uid);
        hql = "SELECT count(0) from qh_user where id=? and DATEDIFF(now(),ModifyDateTime)>=7";
        Object Ocnt=dbServices.findUniqueBySql(hql, intParamMap);
        Integer cnt=Integer.valueOf(Ocnt.toString());
        if(cnt>0){
            resultMap.put("state", "1");
            resultMap.put("msg", "显示唤醒按钮");
            resultMap.put("data", new HashMap());
        }
        else{
            resultMap.put("state", "0");
            resultMap.put("msg", "不显示唤醒按钮");
            resultMap.put("data", new HashMap());
        }
    }
    //信箱是否显示红点
    public void isShowReadDotted(HttpServletRequest request,HttpServletResponse response) throws Exception {
        JSONObject param = CodeHelper.getRequestParams(request);
        Integer uid=param.getInteger("uid");
        i = 0;
        intParamMap.clear();
        intParamMap.put(i++, uid);
        hql = "SELECT count(0) from qh_notice where uid=? and state=99";
        Object Ocnt=dbServices.findUniqueBySql(hql, intParamMap);
        Integer cnt=Integer.valueOf(Ocnt.toString());
        if(cnt>0){
            resultMap.put("state", "1");
            resultMap.put("msg", "显示红点");
            resultMap.put("data", new HashMap());
        }
        else{
            resultMap.put("state", "0");
            resultMap.put("msg", "不显示红点");
            resultMap.put("data", new HashMap());
        }
    }
    public void getDemoList(HttpServletRequest request,HttpServletResponse response) throws Exception {
        hql="from QhDemo";
        List<QhDemo> qhDemos = dbServices.findListByHql(hql, new HashMap());
        resultMap.put("state", "1");
        resultMap.put("msg", "");
        resultMap.put("data", qhDemos);
    }
    public void getDemoView(HttpServletRequest request,HttpServletResponse response) throws Exception {
        JSONObject param = CodeHelper.getRequestParams(request);
        Integer id=param.getInteger("id");
        QhDemo qhDemo = dbServices.getEntity(QhDemo.class, id);
        resultMap.put("state", "1");
        resultMap.put("msg", "");
        resultMap.put("data", qhDemo);
    }
    public void DemoDownLoad(HttpServletRequest request,HttpServletResponse response) throws Exception {
        Date nowTime=new Date();
        SimpleDateFormat time=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String strnowTime= time.format(nowTime);

        JSONObject param = CodeHelper.getRequestParams(request);
        Integer id=param.getInteger("id");
        Integer uid=param.getInteger("uid");

        QhSysPar qhSysPar=dbServices.getEntity(QhSysPar.class, 1);
        QhDemo qhDemo=dbServices.getEntity(QhDemo.class, id);

        double cashMoney=qhDemo.getPrice();

        QhUser qhUser=dbServices.getEntity(QhUser.class, uid);
        String mobile=qhUser.getMobile();

        i=0;
        intParamMap.clear();
        intParamMap.put(i++, id);
        intParamMap.put(i++, mobile);
        String hql="from QhUserProfit where type='下载' and taskId=? and createUser=?";
        List<QhUserProfit> qhUserProfits=dbServices.findListByHql(hql,intParamMap);
        if(qhUserProfits.size()>0){
            resultMap.put("state", "0");
            resultMap.put("msg", "您已下载过");
            resultMap.put("data", new HashMap());
            return;
        }

        double TotalFund=qhUser.getTotalFund() + Double.valueOf(cashMoney);
        double AvailFund=qhUser.getAvailFund() + Double.valueOf(cashMoney);
        qhUser.setTotalFund(TotalFund);
        qhUser.setAvailFund(AvailFund);
        dbServices.update(qhUser);

        List<JSONObject> jsonObjects = Lists.newArrayList();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("uid", uid);
        jsonObject.put("userType", "a");
        jsonObject.put("type", "收益");
        jsonObject.put("title", "您下载了"+qhDemo.getName()+"，获得下载收益"+cashMoney+"元");
        jsonObject.put("note", "您下载了"+qhDemo.getName()+"，获得下载收益"+cashMoney+"元");
        jsonObject.put("createUser", 0);
        jsonObject.put("createDateTime", strnowTime);
        jsonObject.put("modifyUser", 0);
        jsonObject.put("modifyDateTime", strnowTime);
        jsonObject.put("state", 99);
        jsonObjects.add(jsonObject);
        dbServices.saveOrupdateRecords("QhNotice", jsonObjects, null, false);

        jsonObject.clear();
        jsonObjects.clear();
        jsonObject.put("taskId", id);
        jsonObject.put("type", "下载");
        jsonObject.put("cashMoney", cashMoney);
        jsonObject.put("note", "");
        jsonObject.put("createUser", mobile);
        jsonObject.put("createDateTime", strnowTime);
        jsonObject.put("modifyUser", mobile);
        jsonObject.put("modifyDateTime", strnowTime);
        jsonObject.put("state", 99);
        jsonObjects.add(jsonObject);
        dbServices.saveOrupdateRecords("QhUserProfit", jsonObjects, null, false);

        //极光推送
        ToolController.sendMsg("您下载了" + qhDemo.getName() + "，获得下载收益" + cashMoney + "元", mobile);

        resultMap.put("state", "1");
        resultMap.put("msg", "下载成功");
        resultMap.put("data", qhDemo);
    }
    public void isShowDownLoad(HttpServletRequest request,HttpServletResponse response) throws Exception {
        JSONObject param = CodeHelper.getRequestParams(request);
        Integer id =param.getInteger("id");
        Integer uid=param.getInteger("uid");
        QhUser qhUser=dbServices.getEntity(QhUser.class, uid);
        String mobile=qhUser.getMobile();
        i=0;
        intParamMap.clear();
        intParamMap.put(i++, id);
        intParamMap.put(i++, mobile);
        String hql="from QhUserProfit where type='下载' and taskId=? and createUser=?";
        List<QhUserProfit> qhUserProfits=dbServices.findListByHql(hql,intParamMap);
        if(qhUserProfits.size()>0){
            resultMap.put("state", "0");
            resultMap.put("msg", "隐藏下载按钮");
            resultMap.put("data", new HashMap());
        }
        else{
            resultMap.put("state", "1");
            resultMap.put("msg", "显示下载按钮");
            resultMap.put("data", new HashMap());
        }
    }
    public void getInviteList(HttpServletRequest request,HttpServletResponse response) throws Exception {
        hql="from QhInviteList";
        List<QhInviteList> qhInviteLists=dbServices.findListByHql(hql,new HashMap());
        resultMap.put("state", "1");
        resultMap.put("msg", "");
        resultMap.put("data", qhInviteLists);
    }
    public void isShowActivity(HttpServletRequest request,HttpServletResponse response) throws Exception {
        hql="select count(0) from qh_task_centre where now() BETWEEN startDate and endDate and Type='活动任务'";
        Object cnt=dbServices.findUniqueBySql(hql,new HashMap());
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("isShow",cnt);
        resultMap.put("state", "1");
        resultMap.put("msg", "");
        resultMap.put("data", jsonObject);
    }
    public void getActivity(HttpServletRequest request,HttpServletResponse response) throws Exception {
        SimpleDateFormat fmttime=new SimpleDateFormat("yyyy-MM-dd");
        JSONObject param = CodeHelper.getRequestParams(request);
        Integer uid=param.getInteger("uid");
        QhUser qhUser=dbServices.getEntity(QhUser.class, uid);

        hql="from QhTaskCentre where type='活动任务'";
        List<QhTaskCentre> qhTaskCentres=dbServices.findListByHql(hql, new HashMap());

        i=0;
        intParamMap.clear();
        intParamMap.put(i++, qhUser.getMobile());
        intParamMap.put(i++, qhTaskCentres.get(0).getStartDate());
        intParamMap.put(i++, qhTaskCentres.get(0).getEndDate());
        hql="select count(0) from qh_user where inviter=? and createdatetime BETWEEN ? and ? and IsApprentice=1";
        Object cnt=dbServices.findUniqueBySql(hql, intParamMap);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("inviteNum",cnt);
        jsonObject.put("totalNum",qhTaskCentres.get(0).getNum());
        jsonObject.put("startDate",fmttime.format(qhTaskCentres.get(0).getStartDate()));
        jsonObject.put("endDate",fmttime.format(qhTaskCentres.get(0).getEndDate()));
        jsonObject.put("cashMoney",qhTaskCentres.get(0).getCashMoney());
        jsonObject.put("state", Integer.valueOf(cnt.toString()) >= qhTaskCentres.get(0).getNum() ? 1 : 0);

        jsonObject.put("cntDown", qhTaskCentres.get(0).getEndDate().getTime() - System.currentTimeMillis());
        resultMap.put("state", "1");
        resultMap.put("msg", "");
        resultMap.put("data", jsonObject);
    }
    public void receive(HttpServletRequest request,HttpServletResponse response) throws Exception {
        JSONObject param = CodeHelper.getRequestParams(request);
        Integer uid = param.getInteger("uid");
        QhUser qhUser=dbServices.getEntity(QhUser.class, uid);

        i=0;
        intParamMap.clear();
        intParamMap.put(i++, qhUser.getMobile());
        hql="select count(0) from qh_user_profit where createuser=? and type='限时收徒' ";
        Object cnt=dbServices.findUniqueBySql(hql, intParamMap);
        if(Integer.valueOf(cnt.toString())>0){
            resultMap.put("msg", "已领取");
        }
        else {
            resultMap.put("msg", "领取成功");
            QhTaskCentre qhTaskCentre = dbServices.getEntity(QhTaskCentre.class, 8);
            addProfit(uid, 8, "限时收徒", qhTaskCentre.getCashMoney(), qhUser.getMobile(), "");
        }
        resultMap.put("state", "1");
        resultMap.put("data", new HashMap());
    }
    public void createSign(HttpServletRequest request,HttpServletResponse response) throws Exception {
        JSONObject param = CodeHelper.getRequestParams(request);
        Integer uid=param.getInteger("uid");
        SimpleDateFormat fmttime=new SimpleDateFormat("yyyy-MM-dd");

        i=0;
        intParamMap.clear();
        intParamMap.put(i++, uid);
        intParamMap.put(i++, fmttime.format(nowTime));
        Object Ocnt=dbServices.findUniqueBySql("select count(0) from qh_sign where createuser=? and DATE_FORMAT(createdatetime,'%Y-%m-%d')=? and state=99", intParamMap);
        if(Integer.valueOf(Ocnt.toString())>0){
            resultMap.put("state", "0");
            resultMap.put("msg", "");
            resultMap.put("data", new HashMap());
            return;
        }

        i=0;
        intParamMap.clear();
        intParamMap.put(i++, uid);
        Ocnt=dbServices.findUniqueBySql("select count(0) from qh_sign where createuser=? and state=99", intParamMap);

        Integer cnt=Ocnt==null?0:Integer.valueOf(Ocnt.toString());

        List<JSONObject> jsonObjects = Lists.newArrayList();
        JSONObject jsonObject = new JSONObject();

        jsonObject.put("day",cnt+1);
        jsonObject.put("createUser", uid);
        jsonObject.put("createDateTime", strnowTime);
        jsonObject.put("modifyUser", uid);
        jsonObject.put("modifyDateTime", strnowTime);
        jsonObject.put("state", 99);
        jsonObjects.add(jsonObject);
        returnId=dbServices.saveOrupdateRecords("QhSign", jsonObjects, null, false);

        QhSignInfo qhSignInfo=dbServices.getEntity(QhSignInfo.class,cnt+1);
        QhUser qhUser=dbServices.getEntity(QhUser.class,uid);
        addProfit(uid,Integer.valueOf(returnId.toString()),"签到", qhSignInfo.getCashMoney(), qhUser.getMobile(),"");

        resultMap.put("state", "1");
        resultMap.put("msg", "");
        resultMap.put("data", jsonObjects);

    }
    public void getSign(HttpServletRequest request,HttpServletResponse response) throws Exception {
        Date nowTime=new Date();
        SimpleDateFormat time=new SimpleDateFormat("yyyy-MM-dd");
        String strnowTime= time.format(nowTime);

        JSONObject param = CodeHelper.getRequestParams(request);
        Integer uid = param.getInteger("uid");

        JSONObject jsonObject = new JSONObject();

        i = 0;
        intParamMap.clear();
        intParamMap.put(i++, uid);
        hql="select * from qh_sign where createuser=? and state=99 order by day desc LIMIT 1";
        List<QhSign> qhSigns = dbServices.findListBySql(hql, intParamMap, QhSign.class);
        if(qhSigns.size()>0) {
            if (qhSigns.get(0).getDay()==7&&strnowTime.compareTo(time.format(qhSigns.get(0).getCreateDateTime()))>0) {
                hql="from QhSign where createuser=? and state=99";
                List<QhSign> signs = dbServices.findListByHql(hql, intParamMap);
                for(QhSign qhSign:signs){
                    QhSign sign=dbServices.getEntity(QhSign.class,qhSign.getId());
                    sign.setState(-1);
                    dbServices.update(sign);
                }
            }

            if(time.format(qhSigns.get(0).getCreateDateTime()).equals(strnowTime)){
                jsonObject.put("isSign",1);
            }
            else {
                jsonObject.put("isSign",0);
            }
            jsonObject.put("day",qhSigns.get(0).getDay());
            jsonObject.put("signDate",time.format(qhSigns.get(0).getCreateDateTime()));

            i=0;
            intParamMap.clear();
            intParamMap.put(i++, uid);
            intParamMap.put(i++, strnowTime);
            Object Ocnt=dbServices.findUniqueBySql("select count(0) from qh_sign where createuser=? and date_format(createdatetime,'%Y-%m-%d')=? and state=99", intParamMap);
            jsonObject.put("tdIsSign",Ocnt);
            QhSignInfo qhSignInfo=dbServices.getEntity(QhSignInfo.class,qhSigns.get(0).getDay());
            jsonObject.put("cashMoney",qhSignInfo.getCashMoney());
        }
        else{
            jsonObject.put("isSign",0);
            jsonObject.put("day",0);
            jsonObject.put("signDate","");
            jsonObject.put("tdIsSign",0);
            QhSignInfo qhSignInfo=dbServices.getEntity(QhSignInfo.class,1);
            jsonObject.put("cashMoney",qhSignInfo.getCashMoney());
        }

        resultMap.put("state", "1");
        resultMap.put("msg", "");
        resultMap.put("data", jsonObject);
    }
    public void getSignInfo(HttpServletRequest request,HttpServletResponse response) throws Exception {
        hql="from QhSignInfo";
        List<QhSignInfo> signInfos = dbServices.findListByHql(hql, new HashMap());
        resultMap.put("state", "1");
        resultMap.put("msg", "");
        resultMap.put("data", signInfos);
    }
    public void isSign(HttpServletRequest request,HttpServletResponse response) throws Exception {
        Date nowTime=new Date();
        SimpleDateFormat time=new SimpleDateFormat("yyyy-MM-dd");
        String strnowTime= time.format(nowTime);

        JSONObject param = CodeHelper.getRequestParams(request);
        Integer uid = param.getInteger("uid");

        i=0;
        intParamMap.clear();
        intParamMap.put(i++, uid);
        intParamMap.put(i++, strnowTime);
        Object Ocnt=dbServices.findUniqueBySql("select count(0) from qh_sign where createuser=? and date_format(createdatetime,'%Y-%m-%d')=? and state=99", intParamMap);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("state",Ocnt);

        resultMap.put("state", "1");
        resultMap.put("msg", "");
        resultMap.put("data", jsonObject);
    }
    public void getTaskCentre(HttpServletRequest request,HttpServletResponse response) throws Exception {
        SimpleDateFormat fmtTime=new SimpleDateFormat("yyyy-MM-dd");

        JSONObject param = CodeHelper.getRequestParams(request);
        Integer uid = param.getInteger("uid");
        JSONObject jsonObject = new JSONObject();

        i = 0;
        intParamMap.clear();
        intParamMap.put(i++, uid);
        intParamMap.put(i++, "新手任务");
        hql="select a.id,a.Title,a.Type,a.CashMoney,a.Num,a.StartDate,"
        +"a.EndDate,a.BtnText,a.Note,a.CreateUser,a.CreateDateTime,a.ModifyUser,a.ModifyDateTime,"
        +"(select count(0) from qh_user_task_centre where createuser=? and pid=a.id)State"
        +" from qh_task_centre a where type=?";
        List<QhTaskCentre> qhTaskCentres = dbServices.findListBySql(hql, intParamMap, QhTaskCentre.class);
        jsonObject.put("1", qhTaskCentres);

        Object cnt=dbServices.findUniqueBySql("select count(0) from qh_user_task_centre a left join qh_task_centre b on a.pid=b.id where a.createuser=? and b.type=?",intParamMap);
        resultMap.put("isShow",Integer.valueOf(cnt.toString())==5?0:1);

        i = 0;
        intParamMap.clear();
        intParamMap.put(i++, uid);
        intParamMap.put(i++, fmtTime.format(nowTime));
        intParamMap.put(i++, "每日任务");
        hql="select a.id,a.Title,a.Type,a.CashMoney,a.Num,a.StartDate,"
                +"a.EndDate,a.BtnText,a.Note,a.CreateUser,a.CreateDateTime,a.ModifyUser,a.ModifyDateTime,"
                +"(select count(0) from qh_user_task_centre where createuser=? and pid=a.id and date_format(createdatetime,'%Y-%m-%d')=?)State"
                +" from qh_task_centre a where type=?";
        qhTaskCentres = dbServices.findListBySql(hql, intParamMap, QhTaskCentre.class);
        jsonObject.put("2", qhTaskCentres);

        i = 0;
        intParamMap.clear();
        intParamMap.put(i++, uid);
        intParamMap.put(i++, "活动任务");
        hql="select a.id,a.Title,a.Type,a.CashMoney,a.Num,a.StartDate,"
                +"a.EndDate,a.BtnText,a.Note,a.CreateUser,a.CreateDateTime,a.ModifyUser,a.ModifyDateTime,"
                +"(select count(0) from qh_user_task_centre where createuser=? and pid=a.id)State"
                +" from qh_task_centre a where type=?";
        qhTaskCentres = dbServices.findListBySql(hql, intParamMap, QhTaskCentre.class);
        jsonObject.put("3", qhTaskCentres);

        i = 0;
        intParamMap.clear();
        intParamMap.put(i++, uid);
        intParamMap.put(i++, "等级任务");
        qhTaskCentres = dbServices.findListBySql(hql, intParamMap, QhTaskCentre.class);
        jsonObject.put("4", qhTaskCentres);

        resultMap.put("state", "1");
        resultMap.put("msg", "");
        resultMap.put("data", jsonObject);
    }
    public void sendInvite(HttpServletRequest request,HttpServletResponse response) throws Exception {
        JSONObject param = CodeHelper.getRequestParams(request);
        Integer uid = param.getInteger("uid");

        //添加新手任务的首次邀请徒弟
        addUserTaskCentre(uid, 6, "今日邀请收徒",1);
    }
    public void addTaskCentre(HttpServletRequest request,HttpServletResponse response) throws Exception {

        hql="from QhUser";
        List<QhUser> qhUsers = dbServices.findListByHql(hql, new HashMap());
        for(QhUser qhUser:qhUsers){
            if(!StringUtils.isBlank(qhUser.getOpenId())){
                List<JSONObject> jsonObjects = Lists.newArrayList();
                JSONObject jsonObject = new JSONObject();

                jsonObject.put("pid", 2);
                jsonObject.put("createUser", qhUser.getId());
                jsonObject.put("createDateTime", strnowTime);
                jsonObject.put("modifyUser", qhUser.getId());
                jsonObject.put("modifyDateTime", strnowTime);
                jsonObject.put("state", 99);
                jsonObjects.add(jsonObject);
                dbServices.saveOrupdateRecords("QhUserTaskCentre", jsonObjects, null, false);

            }
        }
    }
    //添加任务中心记录
    public void addUserTaskCentre(Integer uid,Integer pid,String type,Integer isDaily){
        SimpleDateFormat fmtTime=new SimpleDateFormat("yyyy-MM-dd");
        QhUser qhUser=dbServices.getEntity(QhUser.class, uid);
        i=0;
        intParamMap.clear();
        intParamMap.put(i++, pid);
        intParamMap.put(i++, uid);
        hql="select count(0) from qh_user_task_centre where pid=? and createuser=?";
        if(isDaily==1){
            intParamMap.put(i++, fmtTime.format(nowTime));
            hql+=" and DATE_FORMAT(createdatetime,'%Y-%m-%d')=?";
        }
        Object Ocnt=dbServices.findUniqueBySql(hql, intParamMap);

        if(Integer.valueOf(Ocnt.toString())==0) {
            QhTaskCentre qhTaskCentre = dbServices.getEntity(QhTaskCentre.class,pid);

            List<JSONObject> jsonObjects = Lists.newArrayList();
            JSONObject jsonObject = new JSONObject();

            jsonObject.put("pid", pid);
            jsonObject.put("createUser", uid);
            jsonObject.put("createDateTime", strnowTime);
            jsonObject.put("modifyUser", uid);
            jsonObject.put("modifyDateTime", strnowTime);
            jsonObject.put("state", 99);
            jsonObjects.add(jsonObject);
            String returnId=dbServices.saveOrupdateRecords("QhUserTaskCentre", jsonObjects, null, false);

            addProfit(uid,Integer.valueOf(returnId.toString()),type, qhTaskCentre.getCashMoney(), qhUser.getMobile(),"");
        }
    }
    public void addCommission() {
        logger.info("2017/09/30测试触发addCommission");
        List<JSONObject> jsonObjects = Lists.newArrayList();
        JSONObject jsonObject = new JSONObject();

        QhSysPar qhSysPar=dbServices.getEntity(QhSysPar.class, 1);

        SimpleDateFormat fmtTime=new SimpleDateFormat("yyyy-MM-dd");
        Date date= ToolController.addDay(new Date(), -1);
        String strDate=fmtTime.format(date);

        hql="from QhSysParLeave";
        List<QhSysParLeave> qhSysParLeaves = dbServices.findListByHql(hql, new HashMap());

        hql="select * from qh_user";
        List<QhUser> qhUsers = dbServices.findListBySql(hql, new HashMap(), QhUser.class);
        for(QhUser qhUser:qhUsers){
            hql = "select sum(CashMoney) from qh_user_profit where CreateUser=? and DATE_FORMAT(CreateDateTime,'%Y-%m-%d')=? and type='分享' and taskid not in (select taskid from qh_task_share where CreateUserLeave=1 and CreateUser=?)";
            i = 0;
            intParamMap.clear();
            intParamMap.put(i++, qhUser.getMobile());
            intParamMap.put(i++, strDate);
            intParamMap.put(i++, qhUser.getMobile());
            Object Omoney1 = dbServices.findUniqueBySql(hql, intParamMap);

            hql = "select sum(CashMoney) from qh_user_profit where CreateUser=? and DATE_FORMAT(CreateDateTime,'%Y-%m-%d')=? and type='下载' ";
            i = 0;
            intParamMap.clear();
            intParamMap.put(i++, qhUser.getMobile());
            intParamMap.put(i++, strDate);
            Object Omoney2 = dbServices.findUniqueBySql(hql, intParamMap);

            hql = "select sum(CashMoney) from qh_user_profit where CreateUser=? and DATE_FORMAT(CreateDateTime,'%Y-%m-%d')=? and type='阅读' ";
            Object Omoney3 = dbServices.findUniqueBySql(hql, intParamMap);

            hql = "select sum(CashMoney) from qh_user_profit where CreateUser=? and DATE_FORMAT(CreateDateTime,'%Y-%m-%d')=? and type='邀请' ";
            Object Omoney4 = dbServices.findUniqueBySql(hql, intParamMap);

            Double cashMoney1=Omoney1==null?0.0:Double.valueOf(Omoney1.toString());
            Double cashMoney2=Omoney2==null?0.0:Double.valueOf(Omoney2.toString());
            Double cashMoney3=Omoney3==null?0.0:Double.valueOf(Omoney3.toString());
            Double cashMoney4=Omoney4==null?0.0:Double.valueOf(Omoney4.toString());
            Double cashMoney=cashMoney1+cashMoney2*qhSysPar.getDownloadPer()+cashMoney3+cashMoney4*qhSysPar.getInviterPer();

            if(cashMoney!=0) {
                String inviter = qhUser.getInviter();

                for (Integer y = 0; y < 9; y++) {
                    if (inviter != null && !inviter.equals("")) {

                        i = 0;
                        intParamMap.clear();
                        intParamMap.put(i++, inviter);
                        hql = "from QhUser where mobile=?";
                        List<QhUser> qhUsers1 = dbServices.findListByHql(hql, intParamMap);
                        if (qhUsers1.size() > 0) {
                            inviter = qhUsers1.get(0).getInviter();

                            if (qhUsers1.get(0).getLeav() != 1 && qhUsers1.get(0).getLeav() > y + 1) {
                                jsonObject.put("uid", qhUsers1.get(0).getId());

                                Double per = 0.0;
                                switch (y) {
                                    case 0:
                                        per = qhSysParLeaves.get(1).getUsePer();
                                        break;
                                    case 1:
                                        per = qhSysParLeaves.get(2).getUsePer();
                                        break;
                                    case 2:
                                        per = qhSysParLeaves.get(3).getUsePer();
                                        break;
                                    case 3:
                                        per = qhSysParLeaves.get(4).getUsePer();
                                        break;
                                    case 4:
                                        per = qhSysParLeaves.get(5).getUsePer();
                                        break;
                                    case 5:
                                        per = qhSysParLeaves.get(6).getUsePer();
                                        break;
                                    case 6:
                                        per = qhSysParLeaves.get(7).getUsePer();
                                        break;
                                    case 7:
                                        per = qhSysParLeaves.get(8).getUsePer();
                                        break;
                                    case 8:
                                        per = 0.0;
                                        break;
                                    default:
                                        per = 0.0;
                                        break;
                                }

                                if(cashMoney * per!=0) {
                                    jsonObject.put("cashMoney", cashMoney * per);
                                    jsonObject.put("note", "");
                                    jsonObject.put("createUser", qhUser.getMobile());
                                    jsonObject.put("createDateTime", strnowTime);
                                    jsonObject.put("modifyUser", qhUser.getMobile());
                                    jsonObject.put("modifyDateTime", strnowTime);
                                    jsonObject.put("state", 99);
                                    jsonObjects.clear();
                                    jsonObjects.add(jsonObject);
                                    dbServices.saveOrupdateRecords("QhUserCommission", jsonObjects, null, false);

                                    jsonObject.clear();
                                    jsonObject.put("taskId", 0);
                                    jsonObject.put("type", "学徒");
                                    jsonObject.put("cashMoney", cashMoney * per);
                                    jsonObject.put("note", "");
                                    jsonObject.put("createUser", qhUsers1.get(0).getMobile());
                                    jsonObject.put("createDateTime", strnowTime);
                                    jsonObject.put("modifyUser", qhUsers1.get(0).getMobile());
                                    jsonObject.put("modifyDateTime", strnowTime);
                                    jsonObject.put("state", 99);
                                    jsonObjects.clear();
                                    jsonObjects.add(jsonObject);
                                    dbServices.saveOrupdateRecords("QhUserProfit", jsonObjects, null, false);

                                    QhUser qhUser1 = dbServices.getEntity(QhUser.class, qhUsers1.get(0).getId());
                                    double TotalFund = qhUser1.getTotalFund() + cashMoney * per;
                                    double AvailFund = qhUser1.getAvailFund() + cashMoney * per;
                                    qhUser1.setTotalFund(TotalFund);
                                    qhUser1.setAvailFund(AvailFund);
                                    dbServices.update(qhUser1);
                                }
                            }
                        }
                    }
                }
            }
        }
        i=0;
        intParamMap.clear();
        intParamMap.put(i++, 0);
        hql="from QhTask where state=?";
        List<QhTask> qhTasks = dbServices.findListByHql(hql, intParamMap);
        for(QhTask qhTask:qhTasks){
            qhTask.setState(1);
            dbServices.update(qhTask);
        }
    }
    public void addCommission1() {
        List<JSONObject> jsonObjects = Lists.newArrayList();
        JSONObject jsonObject = new JSONObject();

        SimpleDateFormat fmtTime=new SimpleDateFormat("yyyy-MM-dd");
        Date date= new Date();
        String strDate=fmtTime.format(date);

        intParamMap.clear();
        hql="from QhSysParLeave";
        List<QhSysParLeave> qhSysParLeaves = dbServices.findListByHql(hql, intParamMap);

        hql="from QhUser";
        List<QhUser> qhUsers = dbServices.findListByHql(hql, intParamMap);
        for(QhUser qhUser:qhUsers){
            hql = "select sum(CashMoney) from qh_user_profit where CreateUser=? and DATE_FORMAT(CreateDateTime,'%Y-%m-%d')=? and type='分享' and taskid not in (select taskid from qh_task_share where CreateUserLeave=1 and CreateUser=?)";
            i = 0;
            intParamMap.clear();
            intParamMap.put(i++, qhUser.getMobile());
            intParamMap.put(i++, strDate);
            intParamMap.put(i++, qhUser.getMobile());
            Object Omoney1 = dbServices.findUniqueBySql(hql, intParamMap);

            hql = "select sum(CashMoney) from qh_user_profit where CreateUser=? and DATE_FORMAT(CreateDateTime,'%Y-%m-%d')=? and type='下载' ";
            i = 0;
            intParamMap.clear();
            intParamMap.put(i++, qhUser.getMobile());
            intParamMap.put(i++, strDate);
            Object Omoney2 = dbServices.findUniqueBySql(hql, intParamMap);

            Double cashMoney1=Omoney1==null?0.0:Double.valueOf(Omoney1.toString());
            Double cashMoney2=Omoney2==null?0.0:Double.valueOf(Omoney2.toString());
            Double cashMoney=cashMoney1+cashMoney2;

            String inviter=qhUser.getInviter();

            for(Integer y=0;y<9;y++){
                if(inviter==null||inviter.equals("")){
                    break;
                }
                i=0;
                intParamMap.clear();
                intParamMap.put(i++, inviter);
                hql="from QhUser where mobile=?";
                List<QhUser> qhUsers1 = dbServices.findListByHql(hql, intParamMap);
                if(qhUsers1.size()>0){
                    inviter=qhUsers1.get(0).getInviter();

                    if(qhUsers1.get(0).getLeav()!=1&&qhUsers1.get(0).getLeav()>y) {
                        jsonObject.put("uid", qhUsers1.get(0).getId());

                        Double per=0.0;
                        switch (y){
                            case 0:
                                per=qhSysParLeaves.get(1).getUsePer();
                                break;
                            case 1:
                                per=qhSysParLeaves.get(2).getUsePer();
                                break;
                            case 2:
                                per=qhSysParLeaves.get(3).getUsePer();
                                break;
                            case 3:
                                per=qhSysParLeaves.get(4).getUsePer();
                                break;
                            case 4:
                                per=qhSysParLeaves.get(5).getUsePer();
                                break;
                            case 5:
                                per=qhSysParLeaves.get(6).getUsePer();
                                break;
                            case 6:
                                per=qhSysParLeaves.get(7).getUsePer();
                                break;
                            case 7:
                                per=qhSysParLeaves.get(8).getUsePer();
                                break;
                            case 8:
                                per=0.0;
                                break;
                            default:
                                per=0.0;
                                break;
                        }

                        jsonObject.put("cashMoney", cashMoney * per);
                        jsonObject.put("note", "");
                        jsonObject.put("createUser", qhUser.getMobile());
                        jsonObject.put("createDateTime", strnowTime);
                        jsonObject.put("modifyUser", qhUser.getMobile());
                        jsonObject.put("modifyDateTime", strnowTime);
                        jsonObject.put("state", 99);
                        jsonObjects.clear();
                        jsonObjects.add(jsonObject);
                        dbServices.saveOrupdateRecords("QhUserCommission", jsonObjects, null, false);

                        jsonObject.clear();
                        jsonObject.put("taskId", 0);
                        jsonObject.put("type", "学徒");
                        jsonObject.put("cashMoney", cashMoney * per);
                        jsonObject.put("note", "");
                        jsonObject.put("createUser", qhUsers1.get(0).getMobile());
                        jsonObject.put("createDateTime", strnowTime);
                        jsonObject.put("modifyUser", qhUsers1.get(0).getMobile());
                        jsonObject.put("modifyDateTime", strnowTime);
                        jsonObject.put("state", 99);
                        jsonObjects.clear();
                        jsonObjects.add(jsonObject);
                        dbServices.saveOrupdateRecords("QhUserProfit", jsonObjects, null, false);

                        QhUser qhUser1=dbServices.getEntity(QhUser.class,qhUsers1.get(0).getId());
                        double TotalFund=qhUser1.getTotalFund() + cashMoney* per;
                        double AvailFund=qhUser1.getAvailFund() + cashMoney* per;
                        qhUser1.setTotalFund(TotalFund);
                        qhUser1.setAvailFund(AvailFund);
                        dbServices.update(qhUser1);
                    }
                }
            }
        }
        i=0;
        intParamMap.clear();
        intParamMap.put(i++, 0);
        hql="from QhTask where state=?";
        List<QhTask> qhTasks = dbServices.findListByHql(hql, intParamMap);
        for(QhTask qhTask:qhTasks){
            qhTask.setState(1);
            dbServices.update(qhTask);
        }
    }
    //添加奖励
    public void addReward(Integer uid) {
        QhUser qhUser=dbServices.getEntity(QhUser.class, uid);

        SimpleDateFormat fmtTime=new SimpleDateFormat("yyyy-MM-dd");
        QhToolCash qhToolCash=dbServices.getEntity(QhToolCash.class, 3);

        i=0;
        intParamMap.clear();
        intParamMap.put(i++, fmtTime.format(qhToolCash.getStartDate()));
        intParamMap.put(i++, fmtTime.format(qhToolCash.getEndDate()));
        intParamMap.put(i++, qhUser.getMobile());

        hql="select a.* from qh_user_profit a inner join qh_user b on a.InviteeMobile=b.mobile where a.type='邀请' and DATE_FORMAT(a.CreateDateTime,'%Y-%m-%d') between ? and ? and a.CreateUser=? and b.leav>1 ";
        List<QhUserProfit> qhUserProfits=dbServices.findListBySql(hql, intParamMap, QhUserProfit.class);
        if(qhUserProfits.size()==qhToolCash.getNumber()){
            List<JSONObject> jsonObjects = Lists.newArrayList();
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("taskId", 0);
            jsonObject.put("type", "奖励");
            jsonObject.put("inviteeMobile", "");
            jsonObject.put("cashMoney", qhToolCash.getCashMoney());
            jsonObject.put("note", "");
            jsonObject.put("createUser", qhUser.getMobile());
            jsonObject.put("createDateTime", strnowTime);
            jsonObject.put("modifyUser", qhUser.getMobile());
            jsonObject.put("modifyDateTime", strnowTime);
            jsonObject.put("state", 99);
            jsonObjects.add(jsonObject);
            dbServices.saveOrupdateRecords("QhUserProfit", jsonObjects, null, false);

            double TotalFund=qhUser.getTotalFund() + qhToolCash.getCashMoney();
            double AvailFund=qhUser.getAvailFund() + qhToolCash.getCashMoney();
            qhUser.setTotalFund(TotalFund);
            qhUser.setAvailFund(AvailFund);
            dbServices.update(qhUser);

            String title="您已完成限时任务【"+qhToolCash.getTitle()+"】";
            String note="完成限时任务【"+qhToolCash.getTitle()+"】，奖励"+qhToolCash.getCashMoney()+"元";
            addNotice(uid,qhUser.getMobile(),"a","收益",title,note,0);
        }
    }

    public void getUrl(HttpServletRequest request,HttpServletResponse response) throws Exception {
        hql="from QhUrl where state=1";
        List<QhUrl> qhUrls = dbServices.findListByHql(hql, new HashMap());
        resultMap.put("state", "1");
        resultMap.put("msg", "");
        resultMap.put("data", qhUrls);
    }
    public void TimeLit_Invite(Integer uid){
        QhUser qhUser=dbServices.getEntity(QhUser.class,uid);
        QhTaskCentre qhTaskCentre=dbServices.getEntity(QhTaskCentre.class,8);
        i=0;
        intParamMap.clear();
        intParamMap.put(i++, qhUser.getMobile());
        intParamMap.put(i++, qhTaskCentre.getStartDate());
        intParamMap.put(i++, qhTaskCentre.getEndDate());
        hql="select count(0) from qh_user_profit where createuser=? and createdatetime BETWEEN ? and ? and type='邀请'";
        Object cnt=dbServices.findUniqueBySql(hql, intParamMap);
        if(Integer.valueOf(cnt.toString())>=2){
            addUserTaskCentre(uid, 8, "限时收徒",0);
        }
    }
    //注册
    public void PubRegister(String inviter,String openid,String picUrl,String mobile,String pwd,String nickName,String sex,String country,String province,String city,HttpServletRequest request){
        JSONObject param = CodeHelper.getRequestParams(request);
        String code=param.getString("code");

        StringBuffer url = request.getRequestURL();
        String tempContextUrl = url.delete(url.length() - request.getRequestURI().length(), url.length()).append(request.getContextPath()).append("/").toString();

        QhSysPar qhSysPar=dbServices.getEntity(QhSysPar.class, 1);
        String inviterNickName="";
        String inviterName="";

        List<JSONObject> qhUser = Lists.newArrayList();
        JSONObject jobj = new JSONObject();

        hql = " from QhUser where uid=? ";
        i=0;
        intParamMap.clear();
        intParamMap.put(i++, inviter);
        List<QhUser> qhUsers = dbServices.findListByHql(hql,intParamMap);
        if(qhUsers.size()>0){
            inviter=qhUsers.get(0).getMobile();
            inviterNickName=qhUsers.get(0).getNickName();
            inviterName=qhUsers.get(0).getName();
        }

        hql = " from QhNoviceTask where state=1 ";
        List<QhNoviceTask> qhNoviceTasks = dbServices.findListByHql(hql,new HashMap());
        for(QhNoviceTask qhNoviceTask:qhNoviceTasks){
            jobj.clear();
            qhUser.clear();
            jobj.put("pid", qhNoviceTask.getId());
            jobj.put("type", qhNoviceTask.getType());
            jobj.put("name", qhNoviceTask.getName());
            jobj.put("cnt", qhNoviceTask.getCnt());
            jobj.put("sigleCashMoney", qhNoviceTask.getSigleCashMoney());
            jobj.put("cashMoney", qhNoviceTask.getCashMoney());
            jobj.put("sort", qhNoviceTask.getSort());
            jobj.put("effectTime", qhNoviceTask.getEffectTime());
            jobj.put("note", qhNoviceTask.getNote());
            jobj.put("createUser", mobile);
            jobj.put("createDateTime", strnowTime);
            jobj.put("modifyUser", mobile);
            jobj.put("modifyDateTime", strnowTime);
            if(qhNoviceTask.getType().equals("注册")){
                jobj.put("state", 1);

                List<JSONObject> jobUsers = Lists.newArrayList();
                JSONObject jobUser = new JSONObject();
                jobUser.put("uid", ToolController.GetRandNum(8));
                jobUser.put("openId", openid);
                jobUser.put("picUrl", picUrl);
                if(inviter!=null&&inviter!=""){
                    jobUser.put("inviter", inviter);
                    jobUser.put("inviterNickName", inviterNickName);
                    jobUser.put("inviterName", inviterName);

                    Operlog(0, "邀请", "register", "inviter", inviter, 1, "", request);

                    //完成奖励任务
                    //comRewardTask_inviter(qhUsers.get(0).getId());
                    //奖励工具提现额度
                    /*hql="select * from qh_tool_cash where type='邀请' and DATE_FORMAT(NOW(),'%Y-%m-%d') between DATE_FORMAT(StartDate,'%Y-%m-%d') and DATE_FORMAT(EndDate,'%Y-%m-%d')";
                    List<QhToolCash> qhToolCashs = dbServices.findListBySql(hql, new HashMap(), QhToolCash.class);
                    if(qhToolCashs.size()>0){
                        i=0;
                        intParamMap.clear();
                        intParamMap.put(i++,inviter);

                        hql="from QhUser where Inviter=?";
                        List userList = dbServices.findListByHql(hql, intParamMap);
                        if(userList.size()==qhToolCashs.get(0).getNumber()) {
                            i=0;
                            intParamMap.clear();
                            intParamMap.put(i++, qhToolCashs.get(0).getId());
                            intParamMap.put(i++,inviter);
                            hql="from QhToolCashUser where pid=? and createUser=?";
                            List CashUserList = dbServices.findListByHql(hql, intParamMap);
                            if(CashUserList.size()==0) {
                                List<JSONObject> jsonQhToolCashUser = Lists.newArrayList();
                                JSONObject jobQhToolCashUser = new JSONObject();
                                jobQhToolCashUser.put("pid", qhToolCashs.get(0).getId());
                                jobQhToolCashUser.put("createUser", inviter);
                                jobQhToolCashUser.put("createDateTime", strnowTime);
                                jobQhToolCashUser.put("modifyUser", inviter);
                                jobQhToolCashUser.put("modifyDateTime", strnowTime);
                                jobQhToolCashUser.put("state", 99);
                                jsonQhToolCashUser.add(jobQhToolCashUser);
                                dbServices.saveOrupdateRecords("QhToolCashUser", jsonQhToolCashUser, null, false);
                            }
                        }
                    }*/
                }
                else{
                    jobUser.put("inviterNickName", "");
                    jobUser.put("inviterName", "");
                }
                jobUser.put("isApprentice", 0);
                if(inviter==null||inviter.equals("")){
                    jobUser.put("inviterIndex", -1);
                }
                else{
                    hql = " from QhUser where inviter=? and islogin=1";
                    i=0;
                    intParamMap.clear();
                    intParamMap.put(i++, inviter);
                    List<QhUser> inviterCnt = dbServices.findListByHql(hql, intParamMap);
                    jobUser.put("inviterIndex", inviterCnt.size()+1);
                    if(inviterCnt.size()+1==1&&code!=null&&!code.equals("")){
                        jobUser.put("isApprentice", 1);
                        //添加新手任务的首次邀请
                        addUserTaskCentre(qhUsers.get(0).getId(), 3, "首次邀请", 0);

                        //添加邀请人收益记录
                        addProfit(qhUsers.get(0).getId(), 0, "邀请", qhSysPar.getInviteCash(), qhUsers.get(0).getMobile(), mobile);

                        //添加新手任务的限时收徒
                        TimeLit_Invite(qhUsers.get(0).getId());
                    }
                }

                if(code==null||code.equals("")){
                    jobUser.put("isLogin", 0);
                }
                else {
                    jobUser.put("isLogin", 1);
                    jobUser.put("srcPla", param.getString("srcpla"));
                }
                jobUser.put("pwd", EncryptUtils.encrypt(pwd));
                jobUser.put("nickName", nickName);
                jobUser.put("name", "");
                jobUser.put("leav", 1);
                jobUser.put("mobile", mobile);
                jobUser.put("sex", sex);
                jobUser.put("birthday", "");
                jobUser.put("idCard", "");
                jobUser.put("alipayName", "");
                jobUser.put("alipay", "");
                jobUser.put("wechat", "");
                jobUser.put("totalFund",0);
                jobUser.put("availFund",0);
                jobUser.put("blockFund",0);
                jobUser.put("isNew",1);
                jobUser.put("isRealName",0);
                jobUser.put("accessToken",ToolController.GetRandLetterString(20));
                jobUser.put("country",country);
                jobUser.put("province",province);
                jobUser.put("city",city);
                jobUser.put("createUser", mobile);
                jobUser.put("createDateTime", strnowTime);
                jobUser.put("modifyUser", mobile);
                jobUser.put("modifyDateTime", strnowTime);
                jobUser.put("state", 1);
                jobUsers.add(jobUser);
                returnId = dbServices.saveOrupdateRecords("QhUser", jobUsers, null, false);

                //设置祖先
                if(inviter!=null&&inviter!="") {
                    getInMobile(Integer.valueOf(returnId), inviter);
                }

                if(code!=null&&!code.equals("")){
                    i = 0;
                    intParamMap.clear();
                    intParamMap.put(i++, inviter);
                    hql = "from QhUserNoviceTask where CreateUser=? and type='邀请' and state=99";
                    List<QhUserNoviceTask> qhUserNoviceTasks = dbServices.findListByHql(hql, intParamMap);
                    if(qhUserNoviceTasks.size()>0){
                        QhUserNoviceTask qhUserNoviceTask=dbServices.getEntity(QhUserNoviceTask.class,qhUserNoviceTasks.get(0).getId());
                        qhUserNoviceTask.setState(1);
                        dbServices.update(qhUserNoviceTask);

                        //升级推荐人
                        upgrade(qhUsers.get(0).getId());
                    }
                }

                QhUser qhUser1=dbServices.getEntity(QhUser.class, Integer.valueOf(returnId));

                resultMap.put("state", "1");
                resultMap.put("msg", "注册成功");
                resultMap.put("data", qhUser1);

                //添加收益记录
                addProfit(Integer.valueOf(returnId),Integer.valueOf(returnId), "首次注册", qhNoviceTask.getSigleCashMoney(), mobile,"");
                //操作日志
                Operlog(0, "首次注册", "register", "firstreg", mobile, 0,"", request);
            }
            else {
                jobj.put("state", 99);
            }
            qhUser.add(jobj);
            dbServices.saveOrupdateRecords("QhUserNoviceTask", qhUser, null, false);
        }

        hql = " from QhToolCash";
        List<QhToolCash> qhToolCashs = dbServices.findListByHql(hql, new HashMap());
        for(QhToolCash qhToolCash:qhToolCashs){
            List<JSONObject> jsonObjects = Lists.newArrayList();
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("pid",qhToolCash.getId());
            jsonObject.put("title",qhToolCash.getTitle());
            jsonObject.put("type",qhToolCash.getType());
            jsonObject.put("rewardType",qhToolCash.getRewardType());
            jsonObject.put("number",qhToolCash.getNumber());
            jsonObject.put("cashMoney",qhToolCash.getCashMoney());
            jsonObject.put("createUser",mobile);
            jsonObject.put("createDateTime",strnowTime);
            jsonObject.put("modifyUser",mobile);
            jsonObject.put("modifyDateTime",strnowTime);
            jsonObject.put("state",99);
            jsonObjects.add(jsonObject);
            dbServices.saveOrupdateRecords("QhUserToolCash", jsonObjects, null, false);
        }
    }

    public void addInMobile(HttpServletRequest request,HttpServletResponse response) throws Exception {
//        hql = "select * from qh_user limit 500,50";
//        List<QhUser> qhUsers = dbServices.findListBySql(hql, new HashMap(), QhUser.class);
//        for(QhUser qhUser:qhUsers){
//            strMobile="";
//            getInMobile(qhUser.getId(),qhUser.getMobile());
//        }
        updateAncester();
        resultMap.put("state", "1");
        resultMap.put("msg", "");
        resultMap.put("data", new HashMap());
    }

    public void updateAncester(){
        List<QhUser> qhUsers = dbServices.findListByHql("from QhUser", new HashMap<>());
//        Map<String, Integer> map = qhUsers.stream().collect(Collectors.toMap(x->x.getMobile(), x->x.getId()));
        qhUsers.stream().filter(x -> StringUtils.isBlank(x.getInviter())).forEach(x -> {
            x.setInMobile("");
            updateAncester(x, qhUsers);
        });

        qhUsers.forEach(x->{
            dbServices.update(x);
        });
    }

    private void updateAncester(QhUser user, List<QhUser> list){
        list.stream().filter(x->!StringUtils.isBlank(x.getInviter()) &&
                x.getInviter().equals(user.getMobile())).forEach(y->{
            y.setInMobile(user.getInMobile()+","+user.getId());
            updateAncester(y, list);
        });
    }

    public void getInMobile(Integer id,String mobile){
        QhUser qhUser=dbServices.getEntity(QhUser.class,id);
        i = 0;
        intParamMap.clear();
        intParamMap.put(i++, mobile);
        hql = " from QhUser where mobile=?";
        List<QhUser> qhUsers = dbServices.findListByHql(hql, intParamMap);
        if(qhUsers.size()>0){
            qhUser.setInMobile((qhUser.getInMobile() == null ? "" : qhUser.getInMobile()) + "," + qhUsers.get(0).getId());
            dbServices.update(qhUser);
            if(qhUsers.get(0).getInviter()!=null&&qhUsers.get(0).getInviter()!=""){
                getInMobile(id,qhUsers.get(0).getInviter());
            }
        }

    }
    public void Operlog(Integer oid,String funName,String funPar,String optName,String mobile,Integer inviteeLeave,String address,HttpServletRequest request){
        JSONObject param = CodeHelper.getRequestParams(request);
        //操作日志
        i=0;
        intParamMap.clear();
        intParamMap.put(i++, mobile);
        hql="from QhUser where mobile=?";
        List<QhUser> qhUsers = dbServices.findListByHql(hql,intParamMap);

        List<JSONObject> jsonQhUserOperLog = Lists.newArrayList();
        JSONObject jobQhUserOperLog = new JSONObject();
        jobQhUserOperLog.put("name", qhUsers.size()>0?qhUsers.get(0).getName():"");
        jobQhUserOperLog.put("oid", oid);
        jobQhUserOperLog.put("funName", funName);
        jobQhUserOperLog.put("funPar", funPar);
        jobQhUserOperLog.put("optName", optName);
        try {
            jobQhUserOperLog.put("srcIp", NetworkUtil.getIpAddress(request));
        } catch (IOException e) {
            e.printStackTrace();
        }
        jobQhUserOperLog.put("address", address);
        jobQhUserOperLog.put("note", "");
        jobQhUserOperLog.put("inviteeLeave", inviteeLeave);
        jobQhUserOperLog.put("srcPla", param.getString("srcpla")==null?"":param.getString("srcpla"));
        jobQhUserOperLog.put("mobileId", param.getString("mobileID")==null?"":param.getString("mobileID"));
        if(qhUsers.size()>0) {
            jobQhUserOperLog.put("createUserLeave", qhUsers.get(0).getLeav());
        }
        jobQhUserOperLog.put("createUser", mobile);
        jobQhUserOperLog.put("createDateTime", strnowTime);
        jobQhUserOperLog.put("modifyUser", mobile);
        jobQhUserOperLog.put("modifyDateTime", strnowTime);
        jobQhUserOperLog.put("state", 99);
        jsonQhUserOperLog.add(jobQhUserOperLog);
        dbServices.saveOrupdateRecords("QhUserOperLog", jsonQhUserOperLog, null, false);
    }
    public void addProfit(Integer uid,Integer taskid,String type,Double cashMoney,String mobile,String inviteeMobile){
        List<JSONObject> jsonQhUserProfit = Lists.newArrayList();
        JSONObject jobQhUserProfit = new JSONObject();

        //修改用户的总资金和余额
        QhUser qhUser=dbServices.getEntity(QhUser.class,uid);
        double TotalFund=qhUser.getTotalFund()+cashMoney;
        double AvailFund=qhUser.getAvailFund() + cashMoney;
        qhUser.setTotalFund(TotalFund);
        qhUser.setAvailFund(AvailFund);
        dbServices.update(qhUser);

        String note="";
        if(type.equals("邀请")){
            note=type+"好友获得收益"+cashMoney+"元";
        }
        else{
            note=type+"任务获得收益"+cashMoney+"元";
        }

        jobQhUserProfit.put("taskId", taskid);
        jobQhUserProfit.put("type", type);
        jobQhUserProfit.put("inviteeMobile", inviteeMobile);
        jobQhUserProfit.put("cashMoney", cashMoney);
        jobQhUserProfit.put("note",note);
        jobQhUserProfit.put("createUser", mobile);
        jobQhUserProfit.put("createDateTime", strnowTime);
        jobQhUserProfit.put("modifyUser", mobile);
        jobQhUserProfit.put("modifyDateTime", strnowTime);
        jobQhUserProfit.put("state", 99);
        jsonQhUserProfit.add(jobQhUserProfit);
        dbServices.saveOrupdateRecords("QhUserProfit", jsonQhUserProfit, null, false);

        List<JSONObject> jsonNotices = Lists.newArrayList();
        JSONObject jsonNotice = new JSONObject();
        jsonNotice.put("uid", uid);
        jsonNotice.put("userType", "a");
        jsonNotice.put("type", "收益");
        jsonNotice.put("title", type+"获得"+cashMoney+"元");
        jsonNotice.put("note", note);
        jsonNotice.put("createUser", 1);
        jsonNotice.put("createDateTime", strnowTime);
        jsonNotice.put("modifyUser", 1);
        jsonNotice.put("modifyDateTime", strnowTime);
        jsonNotice.put("state", 99);
        jsonNotices.add(jsonNotice);
        dbServices.saveOrupdateRecords("QhNotice", jsonNotices, null, false);
        //极光推送
        ToolController.sendMsg(note, mobile);
    }
    public void addNotice(Integer uid,String mobile,String userType,String type,String title,String note,Integer operUid){
        List<JSONObject> jsonNotices = Lists.newArrayList();
        JSONObject jsonNotice = new JSONObject();
        jsonNotice.put("uid", uid);
        jsonNotice.put("userType", userType);
        jsonNotice.put("type", type);
        jsonNotice.put("title", title);
        jsonNotice.put("note", note);
        jsonNotice.put("createUser", operUid);
        jsonNotice.put("createDateTime", strnowTime);
        jsonNotice.put("modifyUser", operUid);
        jsonNotice.put("modifyDateTime", strnowTime);
        jsonNotice.put("state", 99);
        jsonNotices.add(jsonNotice);
        dbServices.saveOrupdateRecords("QhNotice", jsonNotices, null, false);
        //极光推送
        ToolController.sendMsg(note, mobile);
    }
    //新增广告主财务记录
    public void addAdverAccount(Integer uid,Double cashMoney,String mobile){
        List<JSONObject> jsonAdverAccount = Lists.newArrayList();
        JSONObject jobAdverAccount = new JSONObject();
        jobAdverAccount.put("uid", uid);
        jobAdverAccount.put("type", "任务投放");
        jobAdverAccount.put("expend", cashMoney);
        jobAdverAccount.put("createUser", mobile);
        jobAdverAccount.put("createDateTime", strnowTime);
        jobAdverAccount.put("modifyUser", mobile);
        jobAdverAccount.put("modifyDateTime", strnowTime);
        jobAdverAccount.put("state", 99);
        jsonAdverAccount.add(jobAdverAccount);
        dbServices.saveOrupdateRecords("QhAdverAccount", jsonAdverAccount, null, false);
    }

    //完成新手任务，发通告
    public void sendTaskCompleteMsg(Integer uid,String mobile){
        i=0;
        intParamMap.clear();
        intParamMap.put(i++, mobile);
        hql="from QhUserNoviceTask where CreateUser=? and state=99";
        List<QhUserNoviceTask> qhUserNoviceTasks = dbServices.findListByHql(hql, intParamMap);
        if(qhUserNoviceTasks.size()==0) {
            List<JSONObject> notices = Lists.newArrayList();
            JSONObject jobj = new JSONObject();
            jobj.put("uid", uid);
            jobj.put("userType", "a");
            jobj.put("type", "通知");
            jobj.put("title", "您已经完成新手任务，可以提现了");
            jobj.put("note", "您已经完成新手任务，可以提现了");
            jobj.put("createUser", 0);
            jobj.put("createDateTime", strnowTime);
            jobj.put("modifyUser", 0);
            jobj.put("modifyDateTime", strnowTime);
            jobj.put("state", 99);
            notices.add(jobj);
            dbServices.saveOrupdateRecords("QhNotice", notices, null, false);

            //极光推送
            ToolController.sendMsg("您已完成新手任务了，可以提现了", mobile);
        }
    }
    //升级
    public void upgrade(Integer uid){
        System.out.println("陈威平升级陈威平升级陈威平升级");
        QhUser qhUser=dbServices.getEntity(QhUser.class, uid);
        String mobile=qhUser.getMobile();
        Integer leav=qhUser.getLeav();
        Integer leave=leav+1;

        if(leave==10){
            return;
        }

        QhSysParLeave qhSysParLeave=dbServices.getEntity(QhSysParLeave.class, leave);
        Integer InviteCnt=qhSysParLeave.getInviteCnt();
        Integer TaskCnt=qhSysParLeave.getTaskCnt();

        i=0;
        intParamMap.clear();
        intParamMap.put(i++, mobile);
        hql="select count(0) from qh_user where inviter=? and IsApprentice=1";
        if(leave>5){
            intParamMap.put(i++, leave-1);
            hql+=" and Leav>=?";
        }
        Integer InviteCntByUid=0;
        Object OinviteCntByUid=dbServices.findUniqueBySql(hql, intParamMap);
        InviteCntByUid=Integer.parseInt(OinviteCntByUid.toString());

        i=0;
        intParamMap.clear();
        intParamMap.put(i++, mobile);
        hql="select count(0) from qh_task_share where CreateUser=? and IsContinue=0";
        Integer TaskCntByUid=0;
        Object OtaskCntByUid=dbServices.findUniqueBySql(hql, intParamMap);
        TaskCntByUid=Integer.parseInt(OtaskCntByUid.toString());

        if(InviteCnt<=InviteCntByUid&&TaskCnt<=TaskCntByUid){
            if(qhUser!=null){
                qhUser.setLeav(leave);
                dbServices.update(qhUser);

                if(leave==3){
                    //添加新手任务的升级至三尾狐
                    addUserTaskCentre(uid, 9, "升级至三尾狐",0);
                }
                if(leave==4){
                    //添加新手任务的升级至四尾狐
                    addUserTaskCentre(uid, 10, "升级至四尾狐",0);
                }
                if(leave==5){
                    //添加新手任务的升级至五尾狐
                    addUserTaskCentre(uid, 11, "升级至五尾狐",0);
                }

                String inviter=qhUser.getInviter();
                i=0;
                intParamMap.clear();
                intParamMap.put(i++, inviter);
                hql="from QhUser where mobile=?";
                List<QhUser> qhUsers = dbServices.findListByHql(hql, intParamMap);
                if(qhUsers.size()>0) {
                    QhUser qhUser1=dbServices.getEntity(QhUser.class,qhUsers.get(0).getId());
                    if (leave == 3 ||leave==4||leave==5) {
                        double cashMoney=0;
                        String title ="";
                        String note ="";

                        if(leave==3){
                            cashMoney=1.5;
                            title="带徒奖励1.5元";
                            note="徒弟升级到3尾狐后获得奖励1.5元";
                        }else if(leave==4){
                            cashMoney=2.5;
                            title="带徒奖励2.5元";
                            note="徒弟升级到4尾狐后获得奖励2.5元";
                        }else{
                            cashMoney=5;
                            title="带徒奖励5元";
                            note="徒弟升级到5尾狐后获得奖励5元";
                        }
                        List<JSONObject> jsonObjects = Lists.newArrayList();
                        JSONObject jsonObject = new JSONObject();
                        jsonObject.put("taskId", 0);
                        jsonObject.put("type", "学徒");
                        jsonObject.put("inviteeMobile", "");
                        jsonObject.put("cashMoney", cashMoney);
                        jsonObject.put("note", note);
                        jsonObject.put("createUser", qhUser1.getMobile());
                        jsonObject.put("createDateTime", strnowTime);
                        jsonObject.put("modifyUser", qhUser1.getMobile());
                        jsonObject.put("modifyDateTime", strnowTime);
                        jsonObject.put("state", 99);
                        jsonObjects.add(jsonObject);
                        dbServices.saveOrupdateRecords("QhUserProfit", jsonObjects, null, false);

                        double TotalFund = qhUser1.getTotalFund() + cashMoney;
                        double AvailFund = qhUser1.getAvailFund() + cashMoney;
                        qhUser1.setTotalFund(TotalFund);
                        qhUser1.setAvailFund(AvailFund);
                        dbServices.update(qhUser1);

                        addNotice(qhUser1.getId(), qhUser1.getMobile(), "a", "收益", title, note, 0);
                    }

                    //限时任务奖励
                    addReward(qhUser1.getId());
                }

                //极光推送
                ToolController.sendMsg("恭喜您已升级为" + leave + "尾狐", mobile);
            }
        }

    }

    public void comRewardTask_inviter(Integer uid){
        QhUser qhUser=dbServices.getEntity(QhUser.class, uid);
        String mobile=qhUser.getMobile();

        i=0;
        intParamMap.clear();
        intParamMap.put(i++, mobile);
        hql="select count(0) from qh_user where inviter=? and IsApprentice=1";

        Integer InviteCntByUid=0;
        Object OinviteCntByUid=dbServices.findUniqueBySql(hql, intParamMap);
        InviteCntByUid=Integer.parseInt(OinviteCntByUid.toString());

        hql="from QhUserToolCash where type='邀请' and createuser=?";
        List<QhUserToolCash> qhUserToolCashs = dbServices.findListByHql(hql,intParamMap);
        if(qhUserToolCashs.size()>0){
            if(qhUserToolCashs.get(0).getNumber()==InviteCntByUid){
                QhUserToolCash qhUserToolCash=dbServices.getEntity(QhUserToolCash.class,qhUserToolCashs.get(0).getId());
                qhUserToolCash.setState(1);
                dbServices.update(qhUserToolCash);

                if(qhUserToolCashs.get(0).getRewardType().equals("金额")){
                    addProfit(qhUser.getId(),qhUserToolCashs.get(0).getId(),"奖励",qhUserToolCashs.get(0).getCashMoney(), mobile,"");
                }
            }
        }
    }

    public void comRewardTask_share(Integer uid) {
        QhUser qhUser = dbServices.getEntity(QhUser.class, uid);
        String mobile = qhUser.getMobile();

        i=0;
        intParamMap.clear();
        intParamMap.put(i++, mobile);
        hql="select count(0) from qh_task_share where CreateUser=? and IsContinue=0";
        Integer TaskCntByUid=0;
        Object OtaskCntByUid=dbServices.findUniqueBySql(hql, intParamMap);
        TaskCntByUid=Integer.parseInt(OtaskCntByUid.toString());

        hql="from QhUserToolCash where type='分享' and createuser=?";
        List<QhUserToolCash> qhUserToolCashs = dbServices.findListByHql(hql,intParamMap);
        if(qhUserToolCashs.size()>0){
            if(qhUserToolCashs.get(0).getNumber()==TaskCntByUid){
                QhUserToolCash qhUserToolCash=dbServices.getEntity(QhUserToolCash.class,qhUserToolCashs.get(0).getId());
                qhUserToolCash.setState(1);
                dbServices.update(qhUserToolCash);

                if(qhUserToolCashs.get(0).getRewardType().equals("金额")){
                    addProfit(qhUser.getId(),qhUserToolCashs.get(0).getId(),"奖励",qhUserToolCashs.get(0).getCashMoney(), mobile,"");
                }
            }
        }
    }

    public int Getpagecount(int rdcount, int psize)
    {
        float pcount = 0;

        if (rdcount % psize == 0)
        {
            pcount = rdcount / psize;
        }
        else
        {
            pcount = rdcount / psize + 1;
            pcount = (int)pcount;
        }
        return (int)pcount;
    }
    public int Getstartpageindex(int psize, int CurrentPage)
    {
        return CurrentPage * psize - psize;
    }
    public int Getendpageindex(int psize, int CurrentPage, int pcount, int rdcount)
    {
        int Endpid = 0;
        Endpid = CurrentPage * psize;
        return Endpid;
    }

    /**
     * @Method: makeFileName
     * @Description: 生成上传文件的文件名，文件名以：uuid+"_"+文件的原始名称
     * @Anthor:孤傲苍狼
     * @param filename 文件的原始名称
     * @return uuid+"_"+文件的原始名称
     */
    private String makeFileName(String filename){  //2.jpg
        //为防止文件覆盖的现象发生，要为上传文件产生一个唯一的文件名
        return UUID.randomUUID().toString() + "_" + filename;
    }

    /**
     * 为防止一个目录下面出现太多文件，要使用hash算法打散存储
     * @Method: makePath
     * @Description:
     * @Anthor:孤傲苍狼
     *
     * @param filename 文件名，要根据文件名生成存储目录
     * @param savePath 文件存储路径
     * @return 新的存储目录
     */
    private String makePath(String filename,String savePath){
        //得到文件名的hashCode的值，得到的就是filename这个字符串对象在内存中的地址
        int hashcode = filename.hashCode();
        int dir1 = hashcode&0xf;  //0--15
        int dir2 = (hashcode&0xf0)>>4;  //0-15
        //构造新的保存目录
        String dir = savePath + "\\" + dir1 + "\\" + dir2;  //upload\2\3  upload\3\5
        //File既可以代表文件也可以代表目录
        File file = new File(savePath);
        //如果目录不存在
        if(!file.exists()){
            //创建目录
            file.mkdirs();
        }
        return savePath;
    }

}
